1. ## Homework questions?

Can anyone explain to me why I got these ?'s wrong.

given the following 2-dimensional array declaration and inialization:

int a[][4] = {{1,2,3,4}. {5,6,7,8}}

whats the value of the expression *a(a[1]+2)?

i said it was 3 because another way to write it is *(a[1]+2)
which it would add 2 to the first element maiking the answer 3, right.

Q2given the following declarations,

char A[] = "abc", *p="def";

whats the effect of the following statement?

*A =P[2]

i answered it copies the letter 'f' to the first position of A. I said this because *p points to the 2 element which 8A point to the A[] replacing the 2 element with f.

Last ?

given 2 dinensional array declaration:

int A[2][5];

what is the type A[1]?

i said int**. because a fiend tryed to explain to me that each dimension has its own pointer. Which the way he expalined it made alot of sense.

can anyone explain to me please why i got these wrong and whats the correct answers please?

2. I think the first answer is 5.

Second, I think A becomes "abf".

Third, A[1] is an int*. A is an int** so A[1] is an int*.

Just what I would have said.

- Sean

3. >int a[][4] = {{1,2,3,4}. {5,6,7,8}}
This is a syntax error. It has a dot instead of a comma.

>*a(a[1]+2)
I don't believe is valid either.

>char A[] = "abc", *p="def";
>*A =P[2]
.. contains an undefined variable, P. (note the upper case). If it were:
>*A =p[2]
then A would contain fbc.

>int A[2][5];
>what is the type A[1]?
A pointer to an int, or int*. (as already stated).

To answer some of these, why not just write a small prog to see what the answer is?

4. To answer some of these, why not just write a small prog to see what the answer is?
I keep suggesting this, but obviously they think it's better to just ask what the answer is, rather than to actually try and figure it out themselves. And yet, I am not surprised...

Quzah.

5. > 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.

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.

6. Thank you for the clarification Salem