Address of array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Address of array

To print the address we don't use & in printf for strings. Do we use & in printf for array to print the address of array?

24th Sep 2019, 11:18 AM
Shubham Kumar
Shubham Kumar - avatar
4 Answers
+ 5
Address of first element is the address of the array. If you won't dereference, you'll get the addresses of each element of array. In Jay Matthews code, 'a' refers to the address of first index of array and also the address of array.
24th Sep 2019, 11:33 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
~ swim ~ I ran this code, #include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; printf("Sizeof arr: %d\n", sizeof(arr)); printf("Array address: %d\n", arr); printf("Sizeof arr: %d\n", sizeof(arr)); return 0; } This code didn't produce undefined behavior and the array too didn't decay. Then how Jay Matthews code will produce undefined behavior? Or maybe I misunderstood you. Correct me if I am wrong :)
24th Sep 2019, 8:43 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
Now I understand why the behavior will be undefined. Which format specifier should we use to get the array address or is there any other way around?
25th Sep 2019, 11:19 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
Yeah I see but %x should also be not used as it is used for printing unsigned int in hex value.
25th Sep 2019, 7:59 PM
blACk sh4d0w
blACk sh4d0w - avatar