C

Asked • 05/05/19

How come an array's address is equal to its value in C?

In the following bit of code, pointer values and pointer addresses differ as expected.But array values and addresses don't!How can this be?Output my_array = 0022FF00 &my_array = 0022FF00 pointer_to_array = 0022FF00 &pointer_to_array = 0022FEFC<!--formatting fix --> #include <stdio.h> int main() { char my_array[100] = "some cool string"; printf("my_array = %p\\n", my_array); printf("&my_array = %p\\n", &my_array); char *pointer_to_array = my_array; printf("pointer_to_array = %p\\n", pointer_to_array); printf("&pointer_to_array = %p\\n", &pointer_to_array); printf("Press ENTER to continue...\\n"); getchar(); return 0; }

Carter A.

tutor
The compiler "knows" that my_array is an array and is quite capable of treating it differently than a simple variable like pointer_to_array. my_array is a pointer to char and its value is the address of the first element in the array. But if I take the address of my_array, what could it be but the address of the array, specifically its first element.
Report

05/10/19

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.