C Programming : Find the error?

int main()
{
int a[2][2]={
1,2,
3,4
},i,j;

*(a+1)=3;

for(i=0;i<2;i++)
    for(j=0;j<2;j++)
    printf("%d",a[i][j]);

return 0;
}

The above code gives the error : Incompatible types when assigning to type ‘int[2]’ from type int.

What does it mean?
Sorry, i’m very new to programming.

Thankyou!

*(a+1)=3; is wrong.
are you trying to set the a[0][1] to 3 ?
when you use a *(a+1) it will translate to (address of a + 4) which holds another address since its a double array. so you cannot assign an integer to an pointer without deference so this statement could be changed to either ((a+1)) or a[1][0].
if the array would have been a[5] the this statement is valid *(a+1) will translate to *(address of a + 4) which can be assigned to say any int value.

–Prash.

there is a significant difference between the storage of 1-D arrays and 2-D arrays in memory… the name of a 1-D array variable (called a reference for the array) acts as a pointer, which points to the memory address where the first element of the array is stored. But this is not the case in 2-D array. The reference of a 2-D array points to an array of pointers, and each pointer in this array “points” to a 1-D array, forming an array of arrays , called a 2-D array. Here is an image to help you visualize:

![alt text][1]

now, in your code, a is the pointer which points to the first element in the array of pointers, as shown in the image. *a represents the pointer to which ‘a’ points, i.e. the first element in the array of pointers. *(a+1) refers to the 2nd element in the array of pointers. This pointer points to the first element of the second row of the 2-D array, as shown in the image. Hence, *(*(a+1)) will give you the value of a[1][0], i.e. 1st element of 2nd row. Hence, the assignement operation should be like:

*(*(a+1))=3 (or any other value you might want)
[1]: http://discuss.codechef.com/upfiles/img.gif

Actually in this case if u write * ( a + 1 ) then it means u are at second ROW but Column not specified.
that’s why u are getting error.
So simply write * ( * ( a + 1 ) ) = 3 then 3 will be override by 3
and if u write * ( * ( a + 1 ) + 1 ) = 10 then 4 will be override by 10

I can see two mistakes.
firstly the assignment of the 2d array is wrong, refer the below link for correct assignment.

http://www-ee.eng.hawaii.edu/~tep/EE160/Notes/Array/2darray_init.html

secondly where you are using pointer. *(a+1) will give you the value at the address (a+1) . So you cannot assign a value to a value.

Yes you are right about the pointer error but are you sure the assignment of the 2-D array is wrong?

1 Like

Yes, I was trying to modify the a[0][1] using pointer arithmetic. I guess I know my mistake now. Thank you!

What I would have to do for the assignment of a[0][1]?
Thanks for the answer!

You said, * ( * ( a + 1 ) + 1 ) = 10 will give the output as 1 2 3 10.

But can you explain me * ( * ( a + 1 ) + 1 ) ? According to you, the inner * ( a + 1 ) is “The value at the address (a+1)”, which is 3. Right?

So, now we get * ( 3 + 1 ) …which is simply * 4. Which is absurd…

Where i’m wrong?

*((*a)+1)=3;

I’m not getting this solution :frowning:

Whatever enclosed within the *( ) signifies that it is an address. Right?

So, in * ( ( * a ) + 1 ) , ( * a ) + 1 is an address value.

But then how ( * a ) + 1 can possibly be an address, since * a is itself a value at address a.
I’m so confused.

this is the difference between a 1-D array, and a 2-D array… ‘a’ points to an ARRAY OF POINTERS, hence *a represents another pointer, which is the first element of the array of pointers. this pointer (‘*a’), in turn, points to the first element of the first row… (look at the figure CLOSELY)

I got it! " *a represents another pointer " was the key :slight_smile:
Thank you so much!