I am learning pointers in C, so I did this to check out the output I should hope when I do something. Here it is:
int characters(char temp[])
{
printf("\nAddress of string by &temp: %p",&temp);
printf("\nAddress of string by temp: %p",temp);
printf("\nContent of string: %s",temp);
return 0;
}
int main()
{
char s[] = "Prafulla Shahi";
char *t=s;
clrscr();
characters(t);
printf("\nMain\nAddress of string by &s: %p",&s);
printf("\nAddress of string by s: %p",s);
printf("\nContent of string: %s",s);
printf("\nAddress of pointer by &t: %p",&t);
printf("\nAddress of pointer by t: %p",t);
printf("\nContent of pointer: %s",t);
getch();
return(0);
}
The output I get is this:
Address of string by &temp: 0x7fff4ab45788
Address of string by temp: 0x7fff4ab457b0
Content of string: Prafulla Shahi
Main
Address of string by &s: 0x7fff4ab457b0
Address of string by s: 0x7fff4ab457b0
Content of string: Prafulla Shahi
Address of pointer by &t: 0x7fff4ab457a8
Address of pointer by t: 0x7fff4ab457b0
Content of pointer: Prafulla Shahi
My question is: Whys is the address for the temp array show two different values when asked by &temp and temp?
My understanding is, temp is an array variable, but it has its own address (similar behavior to the pointer t. Why does it not behave like the usual variable array s?). The array s[] in main() is an array too, but its address when called by &s and s shows the same value.
Can anyone please explain this?
char s[] = "Hello world";
s is an array object (initialized with a string literal). The address of the array and the address of the first element of the array are the same (only the types are different).
So:
(char *) &s == s /* value of the == expression is 1 */
But:
char *p = "Hello world";
p is a pointer object (pointing to a string literal). The address of p is the address of the pointer object and this address is different than the value of p which is a pointer to the first character of the "Hello world" string.
So:
(char *) &p == p /* value of the == expression is 0 */
To be complete going back to your example I also have to add that an array parameter in a function declaration is adjusted in C to a parameter of a pointer type. So these two function declarations are equivalent:
int characters(char temp[]) { /* ... */ }
and
int characters(char *temp) { /* ... */ }
which also means that inside characters function, we have:
(char *) &temp == temp /* value of the == expression is 0 */
because temp is a pointer object and not an array object.
My understanding is,
tempis an array variable
Unfortunately, no, it isn't.
When you pass an array to a function, it decays into a pointer to its first element. As a quite logical consequence, it was decided that when you declare a function argument like an array, it will be interpreted as a pointer. So in function argument declarations, and only in function argument declarations, the array notation is equivalent with the pointer qualifier. So
int characters(char temp[])
is the same as
int characters(char *temp)
And a pointer is an object that holds a value (an address) and has an own address too, so of course temp is not the same as &temp.
Have you declared temp outside a function argument as a real array (and not a pointer), would your expectation have been met; and then of course &temp would have pointed to the same address as the pointer temp itself decayed into.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With