Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question: about C pointer variable

Status
Not open for further replies.

scottgai

Programmer
Dec 12, 2001
11
US
I was confused about such a fact:
when I declare a array of char type like the following:
char name[30]="scott";
then I have a printf statement:
printf("Addr: %x\n&Addr: %x\n", name, &name);
I got the same value of these two address.
who can tell me the reason about this?
Thanks in advance

 
Replace the first %x with a %s and remove the & on the second name. %x specifies hex output, %s is string.

Matt
 
oh... disregard that, i misread it. I dont know why the pointer to a memory location is equal to a pointer to a pointer to a memory location. I would assume it has something to do with the internals of the compiler.

Matt
 
There are no 2 addresses. Its the same memory location where the array is stored (to be precise, the 1st element of the array is stored)... name and &name point to the same memeory location, and hence the same value. Ankan.

Please do correct me if I am wrong. s-)
 
I agree with what Ankan's said. "name" is the variable name of an array. So "&name" is valued as the address of this variable, it's equivalent to "&name[0]". Things would be different if the variable is defined as:
Code:
char * name = "scott"
This time "name" is the address of the first element of "scott" and "&name" is the address of pointer variable "name".
Thanks Ankan and Matt a lot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top