STTuk
STTuk

Reputation: 55

Is it possible to printf the memory address of the first element of an array in C?

Is it possible to printf the memory address of the first element of an array in C?

The compiler reports an error when I try to:

printf("i%/n", @array[0])

Where I've declared array[] = {23, 56, 78}.

Upvotes: 0

Views: 564

Answers (3)

Yuriko
Yuriko

Reputation: 586

Did you try:

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

?

Upvotes: 5

fuz
fuz

Reputation: 93004

Use the %p formatting specifier. Use & instead of @ to take an address, although this is redundant: &foo[0] is equal to foo except when an operand to sizeof. Technically, you also need to cast the array pointer to void* unless it's a pointer to character (char*) in which case the cast isn't needed:

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

Upvotes: 2

Gopi
Gopi

Reputation: 19864

printf("i%/n", @array[0])

should be

printf("%p\n", (void*)&array[0]);

Use the format specifier p to print address

Upvotes: 4

Related Questions