Velocity Reviews > Address of array behavior in pointer arithmetic

# Address of array behavior in pointer arithmetic

joshc
Guest
Posts: n/a

 06-15-2006
So I've read the relevant sections in the FAQ and the other threads on
this topic but didn't find the exact answer to my question. A fellow
developer wrote the following in some code:

uint16 arr[10];

uint16 val;

val = *(&arr + 0);

So I know that &arr is a pointer to an array of 10 ints versus arr
which is a pointer to int when used in value context. In my case my
compiler ended up storing the address of 'arr' in 'val' for the code
above. Can someone explain what really happens in the code snippet
above? In other words what the (&arr + 0) expression evaluates to in
terms of type, etc.

Thanks.

pete
Guest
Posts: n/a

 06-15-2006
joshc wrote:

> uint16 arr[10];
>
> uint16 val;
>
> val = *(&arr + 0);

> &arr is a pointer to an array of 10 ints

> In other words what the (&arr + 0) expression evaluates to in
> terms of type, etc.

(&arr + 0)
is the same as
&arr

val = *(&arr + 0);
is the same as
val = arr;
and should generate the same warning.

--
pete

jackdorse
Guest
Posts: n/a

 06-15-2006
> uint16 arr[10];
>
> uint16 val;
>
> val = *(&arr + 0);
>
> So I know that &arr is a pointer to an array of 10 ints versus arr
> which is a pointer to int when used in value context. In my case my
> compiler ended up storing the address of 'arr' in 'val' for the code
> above. Can someone explain what really happens in the code snippet
> above? In other words what the (&arr + 0) expression evaluates to in
> terms of type, etc.
>
> Thanks.

arr is a simple pointer of the array. when u say &arr it is something
similar to pointer to pointer.

so when u try to dereference (&arr + 0) it results into an address.
Also, if u look at the warning generated by gcc compiler

"warning: assignment makes integer from pointer without a cast"

makes the point very clear.

deepak
Guest
Posts: n/a

 06-15-2006
As said,
val = arr. But if gives array name, it 'll generate the starting

This one 'll be correct.

main()
{

int val;
int arr[10];

val = **(&arr + 0);

}

Thanks,
Deepak.
jackdorse wrote:
> > uint16 arr[10];
> >
> > uint16 val;
> >
> > val = *(&arr + 0);
> >
> > So I know that &arr is a pointer to an array of 10 ints versus arr
> > which is a pointer to int when used in value context. In my case my
> > compiler ended up storing the address of 'arr' in 'val' for the code
> > above. Can someone explain what really happens in the code snippet
> > above? In other words what the (&arr + 0) expression evaluates to in
> > terms of type, etc.
> >
> > Thanks.

>
> arr is a simple pointer of the array. when u say &arr it is something
> similar to pointer to pointer.
>
> so when u try to dereference (&arr + 0) it results into an address.
> Also, if u look at the warning generated by gcc compiler
>
> "warning: assignment makes integer from pointer without a cast"
>
> makes the point very clear.

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a

 06-15-2006
deepak wrote:
> As said,
> val = arr. But if gives array name, it 'll generate the starting
>
> This one 'll be correct.

Not really.
&arr isn't an int** , it is an int (*)[10].

> main()
> {
>
> int val;
> int arr[10];
>
> val = **(&arr + 0);
>
> }
>
> Thanks,
> Deepak.
> jackdorse wrote:

Chris Dollin
Guest
Posts: n/a

 06-15-2006
joshc wrote:

> So I've read the relevant sections in the FAQ and the other threads on
> this topic but didn't find the exact answer to my question. A fellow
> developer wrote the following in some code:
>
> uint16 arr[10];
>
> uint16 val;
>
> val = *(&arr + 0);

was your fellow developer /thinking/ when they wrote that?

--
Chris "HOT NEWS: x + 0 = x" Dollin
A rock is not a fact. A rock is a rock.

Keith Thompson
Guest
Posts: n/a

 06-15-2006
"jackdorse" <(E-Mail Removed)> writes:
>> uint16 arr[10];
>>
>> uint16 val;
>>
>> val = *(&arr + 0);
>>
>> So I know that &arr is a pointer to an array of 10 ints versus arr
>> which is a pointer to int when used in value context. In my case my
>> compiler ended up storing the address of 'arr' in 'val' for the code
>> above. Can someone explain what really happens in the code snippet
>> above? In other words what the (&arr + 0) expression evaluates to in
>> terms of type, etc.
>>
>> Thanks.

>
> arr is a simple pointer of the array. when u say &arr it is something
> similar to pointer to pointer.

That's wrong. &arr is the address of the array; there's no
pointer-to-pointer anywhere.

(And please don't use silly abbreviations like "u".)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

jackdorse
Guest
Posts: n/a

 06-15-2006

Nils O. Selåsdal wrote:

> deepak wrote:
> > As said,
> > val = arr. But if gives array name, it 'll generate the starting
> >
> > This one 'll be correct.

> Not really.
> &arr isn't an int** , it is an int (*)[10].
>

agreed. srry for the mistake

jackdorse
Guest
Posts: n/a

 06-15-2006

Nils O. Selåsdal wrote:

> deepak wrote:
> > As said,
> > val = arr. But if gives array name, it 'll generate the starting
> >
> > This one 'll be correct.

> Not really.
> &arr isn't an int** , it is an int (*)[10].
>

agreed. thanx for drawing my attention

deepak
Guest
Posts: n/a

 06-15-2006
Nils,

Just want to know mistake was from me or Jachorse?

Chris Dollin wrote:
> joshc wrote:
>
> > So I've read the relevant sections in the FAQ and the other threads on
> > this topic but didn't find the exact answer to my question. A fellow
> > developer wrote the following in some code:
> >
> > uint16 arr[10];
> >
> > uint16 val;
> >
> > val = *(&arr + 0);

>
> Never mind the technical answer to your question: what on /Earth/
> was your fellow developer /thinking/ when they wrote that?
>
> --
> Chris "HOT NEWS: x + 0 = x" Dollin
> A rock is not a fact. A rock is a rock.