K
karthikbalaguru
Hi,
While trying to understand the difference between the following 2
methods, i have some interesting queries.
Method 1) char *s = "Hello";
and
Method 2) char s[] = "Hello";
How does the string 'hello' in first method lie in read-only memory
and the string 'hello' in second method lie in a modifiable memory ?
Only 'const' provides the 'Read-only' functionality in C . How come
this "char *s" provides that functionality ? What is the internal of
this functionality actually ?
The following is the snapshot of the info that has prompted me to
raise this query :-
In any context, char *s = "Hello"; just means that the pointer s is
assigned the address of the string literal "Hello". Normally, that
string literal will reside in read-only memory which means that it's
not legal to do:
char *s = "Hello"; s[1] = 'a';
while it's perfectly legal to do
char s[] = "Hello"; s[1] = 'a';
Thx in advans,
Karthik Balaguru
While trying to understand the difference between the following 2
methods, i have some interesting queries.
Method 1) char *s = "Hello";
and
Method 2) char s[] = "Hello";
How does the string 'hello' in first method lie in read-only memory
and the string 'hello' in second method lie in a modifiable memory ?
Only 'const' provides the 'Read-only' functionality in C . How come
this "char *s" provides that functionality ? What is the internal of
this functionality actually ?
The following is the snapshot of the info that has prompted me to
raise this query :-
In any context, char *s = "Hello"; just means that the pointer s is
assigned the address of the string literal "Hello". Normally, that
string literal will reside in read-only memory which means that it's
not legal to do:
char *s = "Hello"; s[1] = 'a';
while it's perfectly legal to do
char s[] = "Hello"; s[1] = 'a';
Thx in advans,
Karthik Balaguru