Interview Question
Software Engineer / DevelopersTeam: Development
Country: India
This one should give you at least a warning - " char *p = "abc"; " because you are trying to assign a pointer to a non-constant data to a literal constant.
And the "reason behind it" is that the data is constant and cannot be changed.
then what is reason behind char a[3]="abc";
a[0]='z';printf("%s",a);output : zbc.According to you(Ashutosh) what you give , compiler can't change the a[0]='z';but we are getting the output--zbc
char *p = "abc" <--- This is immutable. It's a constant. You cannot change it. If you try in a simple program you will get segfault; if you try to p[0] = 'k';
char p[3] = "abc" --> You declared an array and initialized it. You are allowed to changed array elements.
Following from A book on C:
You need to understand that in C array, pointers and Strings are closely related.
Array name can be used as a pointer. (It's a constant pointer you cannot initialized it)
p[i] and * (p + i) are equivalent
s[i] and *(s + i) are equivalent
++p is valid for increment
++s is wrong --> cannot change s
char *p = "abc" <--- This is immutable. It's a constant. You cannot change it. If you try in a simple program you will get segfault; if you try to p[0] = 'k';
I tried to change p[0]='k' and code was successful compile and got out 'kbc'.
After reading the whole discussion, I got understanding,we can not change the value but I am able to compile and getting result also.
The reason is that p became a constant, but the fundamental reason is that the memory allocated by it is stored in the binary in a section that is not mapped writeable (on Linux it's general .rodata, aka read-only data) hence anything that tries to write in this section will cause a page fault!
abc is a literal and this goes into the read only segment..and when u do a char* p ="abc" then this p points to abc..therefore u cannot change it..On the other hand if u do char p[]="abc" then the literal abc goes to the read only section and a copy of that is assigned to char p[] and henc p[0]= z is allowed in this case
The memory for string is allocated in the read only area of the data region.While memory for the pointer p is allocated in the stack. Hence compiler doesn't permit us to the the string to which the pointer is pointing. To understand & see how it is happening, see the below program & try to analyze how the memory is allocated.
int main()
{
int i;
char *p="Hello";
int j;
printf("%p %p %p %p",&i,&p,&j,p);
return 0;
}
ideone.com/qNREi
Reason for the same is:
- Anonymous May 16, 2012When compiler sees such literals "abc" (or "hello world") it stores them in program space(every running code is divided into program space "where machine code exists and can never be changed during the course of program in execution" and data which has stack, heap etc) and when you do p[0] = 'a' this means you are trying to change something in program space which is restricted....
In case you do something like chap p[] = "abc" then "abc" is copied to data space (either stack if its done inside function call or global data segment if its done globally) hence we can do p[0] = 'b' (because now you are modifying data space..
For the reason given by ashot madatyan: when you try to do p[0] = 'a' its a runtime error(or rather seg fault) and during runtime there is nothing like constant/variable...hence we cant say this is the reason for the same.
Thanks
Ashutosh