1

for example, I have

char* c=(char *)malloc(100*sizeof(char));
printf("0x%x,c);
printf("0x%x,&c);

and this will shows different value for c and &c.

However,if i m having the following:

char d[100];
printf("0x%x,d);
printf("0x%x,&d);

this shows that value of d and &d are the same.

How comes the first code gives me the different result for c and &c?

henryyao
  • 1,758
  • 6
  • 23
  • 37

1 Answers1

6

An array decays into a pointer to its first element in many contexts, including its use in a function call. It doesn't decay when it's the operand of the unary & (address-of) operator. That means d and &d yield the same address in your example, but have different types. char * and char (*)[100], respectively, in your case.

In contrast, c is a pointer. When you take its address with &, you're getting the address of the pointer variable, as opposed to using c directly, which gives you the address it's pointing to. c is a char *, and &c is a char **.

Editorial note: Use %p to print pointer types. %x is for an unsigned int, and unsigned int might be a different size from a pointer. Corrected code might look like:

printf("%p\n", (void *)c);
Carl Norum
  • 219,201
  • 40
  • 422
  • 469