209 views

Assume that $a$ is a one-dimensional array of $4$ elements, $p$ is a pointer variable and $p = a$ is performed. Now, which among these expressions is illegal?

1. $p == a$
2. $p == \&a$
3. $^*p == a$
4. $p == a$

can you please explain, why option a is incorrect
@aarati
Yes, you are correct .
here p is a pointer variable like int *p and p = a is performed.

p holds the address of a, and a would hold the contents at the location.

For option A)
p == a is illegal as p is a pointer variable and holds an address .and a represents the 1st element of array.
so option A is illegal.

For option B and C , both are legal , correct comparison is p == &a, since &a would yield a pointer that points to first element of 'a'.

And *p pointing  the value holds by a .

For option D)  it is also legal.

p == a is correct and true since in this case both the expressions p and a yield the content of the first element of array a.

@Bikram sir as it is mentioned in question that

a is a one-dimensional array of 4 elements

it may be the case that this array "a"  is array of pointers and each element of this is pointing to addresses of differnt elements.Then if suppose a is holding the address of an variable x;

then p==a will be a legal action because both are addresses.

it may be the case that this array "a"  is array of pointers

@ reena_kandari i don't think we can assume ourself whatever we want,they will specify if it is an array of pointers or atleast they give declaration like int *a.

p is a pointer variable it means int *p;

after that p=a;

• statement 1)p==a

p is holding address and a is holding value how this can be done . // This is illegal

• statement 2)

p==&a

p==&*(a+0)

• statement 3) *p ==a

both are holding 1st value of array a .

• statement 4) p==a same as previous

Therefore  statement 2,3,4 is right

@jai ,

yes, you are correct .

int *p;

int a;

p = a; // so now value of p is address of first element of the array a .

then we can compare both first element like that p == a  // so it is a valid statement

p holds the address of a, and a would hold the contents at the location. so p ==a would not possible

as p holds address and a holds value of first element of array , we can not compare them. i am still getting error?

1
224 views