C Programming Help Please!

CM4

Member
Jun 9, 2001
64
0
0
I was just hoping if you could tell me if these two C statements are equivalent to one another.

Given the following definitions:

int ar[] = {1, 2, 4, 816};
int * ptr;

Consider the two statements

ptr = &ar [ 0 ];
ptr = ar;;

Are the two statements equivalent to one another? Please could you explain why they are OR why they are not?

Thanks. :)
 

br0wn

Senior member
Jun 22, 2000
572
0
0
yes, they are equivalent.
Why?

The statement ptr = &ar[ 0 ]; means that
you set ptr to the address of the first element of array ar
(which is the address of ar).

The statement ptr = ar; means that you set
ptr to ar (where ar holds the address of array ar or
it holds the address of the first element of array ar).

Hey, stop posting your homework questions.

 

CM4

Member
Jun 9, 2001
64
0
0
Thanks again br0wn! Your great.

PS They are not homework questions.

They are past exam paper questions. I am just trying to work them out because the real exam should have similar questions. Their was no marking scheme provided so I thought I would ask the kind Anandtechers for help.

Hope I am not being a pain.