Sign in to follow this  
Ned_K

Question on the type of &arr when arr is an array of ints

Recommended Posts

Ok, another theory question. I know this is nitpicking but these details bother me if I don't feel like I have them down cold. Here goes. From Summit's c-faq is this statement:
Quote:
Q: Since array references decay into pointers, if arr is an array, what's the difference between arr and &arr? ________________________________________ A: The type. In Standard C, &arr yields a pointer, of type pointer-to-array-of-T, to the entire array. (In pre-ANSI C, the & in &arr generally elicited a warning, and was generally ignored.) Under all C compilers, a simple reference (without an explicit &) to an array yields a pointer, of type pointer-to-T, to the array's first element. For a simple array int a[10]; a reference to a has type “pointer to int,” and &a is “pointer to array of 10 ints.”
Ok, if that is the case, why does this code work? void main(){ int *p; int arr[10] = {0,1,2,3,4,5,6,7,8,9}; p = &arr; printf("%d\n", *(p+1)); } My Question: If p is of type pointer to int, how can it be set to point to "array of 10 ints" as is decribed in the faq response? Point it to a single element, yes, I'm good. Point it to arr using p = arr; and it points to arr[0], and again I am ok. But the wording he used in that faq makes me want to think that there is a type conflict of some sort with p = &arr; p should point to individual integers given my code, not "array of 10 ints" as is described in the faq...or at least that's how it feels. Seems to me the only thing that can actually be a "pointer to array of 10 ints" is arr itself. I.e. this is an array-specific type reserved only for the designator of the array. Or am I reading vastly more into this than I should?

Share this post


Link to post
Share on other sites
That code should not work, and should not compile, taking the address of arr yields an int**, not and int*. It doesn't compile for me anyway, I would hope there isn't a compiler out there that would let that go.

Share this post


Link to post
Share on other sites
Quote:
Original post by emeyex
That code should not work, and should not compile, taking the address of arr yields an int**, not and int*.


Good, so I'm not insane.

But wouldn't that in theory still be an issue? &arr is a type pointing to the entire array of 10 ints. int** points to a pointer that points to an int.

Share this post


Link to post
Share on other sites
I'm not sure what the issue is. That code can never happen under a valid compiler, so there's no issue there. Are you talking about doing this:

int arr[10];
int* p = arr;

?

Share this post


Link to post
Share on other sites
Quote:
Original post by emeyex
I'm not sure what the issue is. That code can never happen under a valid compiler, so there's no issue there. Are you talking about doing this:

int arr[10];
int* p = arr;

?


It was my bad. The compiler was defaulting to the old code and I didn't realize it. That code in the OP does fail.

Share this post


Link to post
Share on other sites
Quote:
Original post by emeyex
That code should not work, and should not compile, taking the address of arr yields an int**, not and int*. It doesn't compile for me anyway, I would hope there isn't a compiler out there that would let that go.


No, taking the address of arr yields an int (*)[10], not an int * *.

Σnigma

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