685 views
2 2 votes
char a[]="gateoverflow";

char *p="gateoverflow";

Why even though both are same way if representing string .By second way we cannot change the value???

1 Answer

0 0 votes

1. char a[]="gateoverflow";

  • "gateoverflow" is stored in Stack Frame, which is read-write memory, hence you can easily modify the contents of the string.

2. char *p="gateoverflow";

  • the location of "gateoverflow" is in the executable, and the location a points to, is in the executable. The data in the executable image is read-only, hence you can't modify it.

ref: https://stackoverflow.com/questions/1011455/is-it-possible-to-modify-a-string-of-char-in-c#

Position:
Show:

Related questions

8 8 votes
1 1 answer
261
261 views
GO Classes asked Jun 19
261 views
What is the output of the following code?#include <stdio.h void fun(int a[], int *p) { a = a + 5; *(p + 2) = *(p + 2) + 10; p++; *p = *p + 1; } int main() { int arr[] =...
6 6 votes
1 1 answer
209
209 views
GO Classes asked Jun 19
209 views
What is the output of the following code?#include <stdio.h int main() { int arr[] = {10, 20, 30}; int *p = arr; printf("%d %d", *p, *(arr + 2)); return 0; }$\texttt{10 20...
7 7 votes
1 1 answer
219
219 views
GO Classes asked Jun 6
219 views
What is the output of the following code?#include <stdio.h void change(int *p, int *q) { *p = *p + 4; *q = *p + *q; p = q; *p = *p - 3; } int main() { int a = 6, b = 10; ...
7 7 votes
1 1 answer
227
227 views
GO Classes asked Jun 6
227 views
What is the output of the following code?#include <stdio.h int main() { int a = 8, b = 12; int *p = &a; int *q = &b; *p = *p + *q; q = p; *q = *q - 5; printf("%d %d", a, ...