Reputation: 325
How is it possible, name of string contains address of string - YES ok but its (name of string) address should be different, but it is the same
NOT only that , *str
gives the first character - that's ok
but *str
is nothing but value at(6356737), in this code
but that itself is equal to 6356737 and not 'f'(which is nothing but str[o])
int main()
{
char str[] = "fdsgugreui";
printf("\nstr=%u,&str=%u : *str = %c\n",str,&str,*str);
int i=0;
while(str[i] != '\0'){
printf("\n&str[%d] =%u : str[%d] = %c\n ",i, &str[i], i, str[i]);
++i;
}
return 0;
}
//result...
str=6356737,&str=6356737 : *str = f
&str[0] =6356737 : str[0] = f
&str[1] =6356738 : str[1] = d
&str[2] =6356739 : str[2] = s
&str[3] =6356740 : str[3] = g
&str[4] =6356741 : str[4] = u
&str[5] =6356742 : str[5] = g
&str[6] =6356743 : str[6] = r
&str[7] =6356744 : str[7] = e
&str[8] =6356745 : str[8] = u
&str[9] =6356746 : str[9] = i
Process returned 0 (0x0) execution time : 0.150 s
Press any key to continue.
I cannot understand why!
Upvotes: 0
Views: 531
Reputation: 30926
str
is the char array which decays into a pointer to the first element of the char
array - which is same as &str[0]
.
Note that value-wise str
and &str
are the same but their type is different. str
decays into pointer - so it becomes char*
but the second one is char(*)[11]
(note when you pass array name as operand of &
it won't decay into pointer) which is a pointer to an array object.
The correct way to print address is printf("%p",(void*)str);
and this also will be same for other pointer variables.
str[0]
is the content of the array str
at the position 0
. That's it - it has nothing to do with having an address, It is a char
. In your case str[0]
is nothing but 'f'
.
What is array decaying?
So in most situations, the array is converted to the pointer to the first element. This is array decaying. Notice the case here str
is an array of char
s - when we use str
in printf
it is converted to a pointer to the first element of itself which is address of str[0]
and the pointer contains that value. This explains why you get the value of &str[0]
when you printed str
.
There are a few exceptions to array decaying:
sizeof
operator. _Alignof
operator, or &
operator, or The fun part is &str
here you see it is an operand to &
address of operator - this is an exception where the decaying won't happen. So &str
is a pointer to an array object. The type is char (*)[]
. And yes, as you sae it will be of the same value as that of str
or &str[0]
but it's type is completely different.
What is char(*)[SIZE]
?
It is denoting a pointer to an array of chars which has the size of SIZE
. There are SIZE
elements in the array.
Is char (*p)[SIZE]
same as char* p[SIZE]
?
Noo. It is not. First one is a pointer to an array of char
s which has SIZE
number of elements.
char* p[]
is an array of char*
-s. These are not the same. The first one is denoting a single pointer and the second is an array of pointers.
Another thing is - for pointers there are two things that matter
The second one dictates how the pointer arithmetic will be depending on the size of the type of object it points to. In here also you saw that &str
and str
has same value but not the same type. You will be surprised if you see these statement and print it.
printf(" &str+1 = %p str+1 = %p\n",(void*)(&str+1),(void*)(str+1));
Hint: The first one
&str
is a pointer to an array. The second is pointer to achar
.
Upvotes: 2