c - what's the difference between the return value of a malloc() and an array name -


this question has answer here:

for example, have

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

and shows different value c , &c.

however,if m having following:

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

this shows value of d , &d same.

how comes first code gives me different result c , &c?

an array decays pointer first element in many contexts, including use in function call. doesn't decay when it's operand of unary & (address-of) operator. means d , &d yield same address in example, have different types. char * , char (*)[100], respectively, in case.

in contrast, c pointer. when take address &, you're getting address of pointer variable, opposed using c directly, gives address it's pointing to. c char *, , &c char **.

editorial note: use %p print pointer types. %x unsigned int, , unsigned int might different size pointer. corrected code might like:

printf("%p\n", (void *)c); 

Comments