c - Array is treated as constant pointer,so we cannot change the pointer value but individually element of an array can be changed why? -


array treated constant pointer,so cannot change pointer value,i unable figure out lets suppose-

  a[5]="hello";   a="hai"; // know not possible why if below a[0]='a' possible?   a[0]='a' ;//this possible because store in stack memory. 

now coming pointer

 char *a="hello";   a="hai"; //i know possible because string store in code section read memory why changed?     a[0]='a' //not possible  because store in stack , string store in code section if above a="hai" possible why a[0]='a' not possible? 

i new here , have been blocked 2 times please apart demoralizing making vote down please me understand concept

assume following declarations:

char arr[6] = "hello"; char *ap    = "hello"; 

here's 1 possible memory map showing outcome of declarations (all addresses made out of thin air , don't represent real-world architecture):

item            address        0x00 0x01 0x02 0x03 ----            -------        ---- ---- ---- ---- "hello"         0x00008000      'h'  'e'  'l'  'l' "hai"           0x00008004      'o' 0x00  'h'  'a'                 0x00008008      'i' 0x00 0x?? 0x??                 ... arr             0x82340000      'h'  'e'  'l'  'l'                 0x82340004      'o'  0x00 0x?? 0x?? ap              0x82340008      0x00 0x00 0x80 0x00 

the string literal "hello" stored 6-element array of char static storage duration @ address 0x0008000. string literal "hai" stored 4-element array of char static storage duration @ address 0x00080006. contents of string literal may stored in read-only section of memory (depends on platform). behavior on attempting modify contents of string literal undefined; code considered erroneous, compiler isn't required handle situation in particular way. may segfault, or change won't applied, or change applied expect. i'm assuming multiple instances of string literal in source code map single instance in memory (which usual case ime).

except when operand of sizeof or unary & operators, or string literal being used initialize array in declaration, expression of type "n-element array of t" converted ("decay") expression of type "pointer t", , value of expression address of first element of array. resulting pointer non-modifiable lvalue.

the declaration

char arr[6] = "hello"; 

declares arr 6-element array of char auto extent, , contents of string literal "hello" copied it. can modify values of arr[0] through arr[5] want, can write

a[0] = 'a'; 

but cannot write like

arr = "hai"; 

because while both of expressions arr , "hai" converted pointer values described above, arr isn't pointer variable; there's no storage associated arr apart array elements themselves, there's no place assign result of "hai" (0x00008006).

the declaration

char *ap    = "hello"; 

declares ap pointer char, , assigns result of expression "hello" it. in case, string literal not operand of sizeof or unary & operators, , isn't being used initialize contents of array, converted pointer expression, , value address of first element of array, or 0x0008000.

this mirror opposite of arr; cannot update contents of ap[0] through ap[5], can assign new pointer value ap,

ap = "hai"; 

works. after doing so, memory map like

item            address        0x00 0x01 0x02 0x03 ----            -------        ---- ---- ---- ---- "hello"         0x00008000      'h'  'e'  'l'  'l' "hai"           0x00008004      'o' 0x00  'h'  'a'                 0x00008008      'i' 0x00 0x?? 0x??                 ... arr             0x82340000      'a'  'e'  'l'  'l'                 0x82340004      'o'  0x00 0x?? 0x?? ap              0x82340008      0x00 0x00 0x80 0x06 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -