Sign in to follow this  
Ned_K

Trivial array question

Recommended Posts

Given this simple code: char stay[1] = "s"; int query[1] = {1}; printf("%d\n", *query); printf("%d\n", query); printf("%d\n", query[0]); printf("%p\n", &query); printf("%p\n", &query[0]); printf("%c\n", *stay); printf("%c\n", stay); printf("%c\n", stay[0]); printf("%p\n", &stay); printf("%p\n", &stay[0]); I get this output: 1 1244792 1 0012FE78 0012FE78 s ç s 0012FE87 0012FE87 Ok, so here is my question: If stay and stay[0] both represent the first element of the array, why doesn't stay contain the same thing that stay[0] contains? They both have the same memory address. Why does printf("%d\n", query) output 1244792 (the base 10 version of the address 0012FE78 for the first element of the query array) while printf("%c\n", stay) produces a character that seems to have no relation to the address of the first element of stay at all?

Share this post


Link to post
Share on other sites
*stay and stay[0] represent the same information [the value of the first element of the array]. Likewise, stay and &(stay[0]) represent the same information [the address of the first element of the array].

printf("%c\n", stay); is outputing a character because you are asking it to. printf("%p\n", stay); yields the result you expect.

CM

Share this post


Link to post
Share on other sites
With the first line you just overwrote some memory you didn't allocate with a zero. If you want a string, do char stay[2] = "s";. If you want an array of characters, do char stay[1] = {'s'};.

stay and stay[0] do not represent the same thing. *stay and stay[0] do.

The character output with the %c doesn't seem to be related because the 32-bit address was truncated to 8 bits and then converted to a character.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ned_K
Ok, so here is my question:
If stay and stay[0] both represent the first element of the array, why doesn't stay contain the same thing that stay[0] contains? They both have the same memory address.

stay and stay[0] do not both represent the first element of the array. stay[0] is the first element of the array, stay is the address of the first element of the array (and the same for query and query[0]; query[0] is the first element, query is the address).

Quote:
Why does printf("%d\n", query) output 1244792 (the base 10 version of the address 0012FE78 for the first element of the query array) while printf("%c\n", stay) produces a character that seems to have no relation to the address of the first element of stay at all?

Actually, printf("%c\n", stay); is outputting a character that is related to the address of stay. Specifically, it's outputting the character with value 0x87, which is the last byte of the address that stay contains (0012FE87).

It won't output the whole address as a character, because the %c formatting code tells it that you are passing it a single character value (1 byte), and it therefore only uses one byte of whatever value you actually give it.

John B

Share this post


Link to post
Share on other sites
Ok, finished my Thanksgiving drive back so I actually have questions now.

How can stay and stay[0] have the same address if they each contain different information? Both share the address 0012FE87 yet each contains something unique from the other one, stay contains the address of the first element of the array and stay[0] the value in the array at 0. With a pointer, let's say *p, p itself contains the address of the object pointed to and has an address of its own. What am I not understanding on this? I know it's pretty basic.

I find differing degrees of straw splitting as to the exact differences between arrays and pointers in C, so please forgive my thick-headedness.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ned_K
Ok, finished my Thanksgiving drive back so I actually have questions now.

How can stay and stay[0] have the same address if they each contain different information? Both share the address 0012FE87 yet each contains something unique from the other one, stay contains the address of the first element of the array and stay[0] the value in the array at 0. With a pointer, let's say *p, p itself contains the address of the object pointed to and has an address of its own. What am I not understanding on this? I know it's pretty basic.


Basically, the answer is: Because 'stay' is on the stack.

If the array was allocated on the heap, then you would have to access it through an actual pointer, in which case that pointer would have its own address in memory. However, because stay is on the stack, the compiler knows where it is, so it doesn't need to actually store that address anywhere (there is no actual variable that contains the address of the 'stay' array data). However, because of the semantics of the language, when you use 'stay' as a variable on its own [eg, printf("%p", stay);], the compiler needs to provide a value, and the value it uses is the address of the array data on the stack. When you use the address-of operator on 'stay' [eg, printf("%p", &stay);] the compiler again needs to provide a value - the only value that makes sense is again the address of the array data on the stack.

In contrast, if the array was on the heap, it might look more like this:

int an_array[];

an_array = new int[10];

for (int i = 0; i < 10; ++i)
an_array[i] = i+1;

printf("%p", &an_array);
printf("%p", an_array);
printf("%p", &an_array[0]);
printf("%d", an_array[0]);

// might print something like:
0012FF63
01AB6A9F
01AB6A9F
1

// note that in this case, the &an_array is not the same as an_array
// because it was allocated on the heap, there must be an actual pointer stored
// &an_array gives you the address of that pointer (on the stack)
// an_array gives you the address of the array data (on the heap)




John B

Share this post


Link to post
Share on other sites
Quote:
How can stay and stay[0] have the same address if they each contain different information?

They don't.
You use & to get the address of a variable. If you printf stay, you don't get the address of stay. You get the value contained in stay.
So try printf'ing &stay and &(stay[0]) respectively.
They'll be different.

stay is a variable which contains an address (It's a pointer). The address it contains is the address of stay[0]. (Which contains the first actual value in the array)

Quote:

I find differing degrees of straw splitting as to the exact differences between arrays and pointers in C, so please forgive my thick-headedness.

In C/C++, it is basically a question of syntax, nothing more. Under the hood, it *is* a pointer. But the syntax is different.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
Quote:
How can stay and stay[0] have the same address if they each contain different information?

They don't.
You use & to get the address of a variable. If you printf stay, you don't get the address of stay. You get the value contained in stay.
So try printf'ing &stay and &(stay[0]) respectively.
They'll be different.

Unfortunately, it's not quite that simple if the array is on the stack. For example, try this:

#include <stdio.h>

int main(int argc, char *argv[])
{
int arr[5];
printf("&(arr[0]) = %p\n", &(arr[0]));
printf(" arr = %p\n", arr);
printf(" &arr = %p\n", &arr);
return 0;
}



The output I got was this:

&(arr[0]) = 0x23eef0
arr = 0x23eef0
&arr = 0x23eef0



John B

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this