> whats the value of the expression *a(a[1]+2)?
meaningless, because it thinks 'a' is a function pointer, which in this instance it clearly isn't.
Oooh, I can try code
Code:
#include <stdio.h>
int main (void)
{
int a[][4] = {{1,2,3,4}, {5,6,7,8}};
char b[] = "abc", *p="def";
printf( "Value of a[1][2] is %d\n", a[1][2] );
printf( "Value of *(a[1]+2) is %d\n", *(a[1]+2) );
printf( "b initially %s\n", b );
*b = p[2];
printf( "A is now %s\n", b );
return 0;
}
> given 2 dinensional array declaration:
> int A[2][4];
> what is the type A[1]?
It seems everyone has gotten this wrong so far. Hammer is closest, but only in certain circumstances.
First of all, read this
The type of A is int[2][4]
The type of A[1] is int[4]
The type of A[1][0] is int
See the pattern?
If you draw the array on paper, there are no pointers (its just a contiguous block of memory), so there can be no answer which contains a *
Since C doesn't support array assignments, we can't do this
int b[4] = A[1];
So we rarely see the type of A[1] in actual use, but it's there.
But
int b = A[1][0];
is real enough.
However, things change slightly when you pass arrays as parameters to functions.
When you pass an array as a parameter, you get a pointer to the first element of the array, so
void foo ( int *);
foo ( A[1] );
would be correct
Here's some code which will probably confuse initially...
Code:
int (*p1)[4] = &a[1]; // point to the whole of a[1]
int *p2 = a[1]; // point to the start of a[1]
int i;
for ( i = 0 ; i < 4 ; i++ ) {
printf( "%d: %d %d\n", i, p1[0][i], p2[i] ); // using array notation
printf( "%d: %d %d\n", i, (*p1)[i], *(p2+i) ); // using pointer notation
}
Ok, as I've said before, a[1] is an int[4], so &a[1] is a pointer to an int[4] (which is what p1 is declared as). This is about as close as you can get to actually seeing int[4] as a type in actual use.
p2 mimics what happens if you were to pass a[1] as a parameter to a function (namely a pointer to the first element)
The for loop illustrates the many ways of dereferencing those pointers to yeild the stored values.
So what's the point of p1?
Well if you want to dynamically allocate an array which is
int arr[x][4];
then p1 is the type of the pointer which you must allocate.