# Thread: help on test quest

1. ## help on test quest

If we declare: int a[4][3]; then a[i][j] is equivalent to:
1) *(a[i] + j)
2) *(&a[0][0] + 4*i + j)
3) Both (a) and (b)
4) None of the above

Can anyone enlighten me on which is the correct ans and
why !! thanks

2. I'll give you two hints...

1. an array name is actually a pointer to the first position in the array

2. A multidimensional array is in reality an array of arrays. And to dig in even further, a multidimensional array is in fact a single dimension array. arr[2][3] is in fact an array with 6 elements

3. Originally Posted by Mario F.
And to dig in even further, a multidimensional array is in fact a single dimension array. arr[2][3] is in fact an array with 6 elements
Really? Does arr[1][1] equal ((int*)arr)[4]?

A multidimensional array isn't usually a single dimension array. Consider argv. Each argument is a different length.

4. Yes. I should have rephrased that better. I meant conceptually since,

Code:
```int arr[4][3] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} };

//and

int arr[4][3] = {1,2,3,4,5,6,7,8,9,10,11,12};```
produce the same array.

The second definition helps making it clear which answer is the right one.

5. > Consider argv. Each argument is a different length.
True, each argument is a different length, but you are a little confused about argv. argv is an array of pointers to strings.

You can find the type of a declaration by reading parentheses (if there are any) inside out and right to left.

6. arr[2][3], and arr[6] produce essentially the same array. In memory there is actually no difference. C++ strictly speaking doesn't know multi-dimension arrays.

Besides b) is wrong

7. hi mario. from ur explaination 1) is rt

but for *(&a[0][0] + 4*i + j)
i am not really sure.

my feeling is that it is wrong .

and the rt ans would be a[i][j] =*(&a[0][0] + 3*i + j)
is this correct??

8. almost

*(&a[0][0] + 4*i + j -1)

Which makes it a too complicated formula.

You are correct. a) is right. All others are wrong

9. > but for *(&a[0][0] + 4*i + j)
Right away, this is dereferencing a memory address, which will make the program explode. It is wrong.

10. arr[2][3], and arr[6] produce essentially the same array. In memory there is actually no difference. C++ strictly speaking doesn't know multi-dimension arrays.
I think it is more like the computer does not know about multi-dimensional arrays, and C++ initializer syntax allows one to take advantage of that as a shorthand. C++ does know about multi-dimensional arrays.

11. hi guys i have written a prog that proves 2)
is correct. the output is 1.
IS there anything wrong with my prog??
Code:
```#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
int a[4][3],i,j;
int test();
main(void)
{
printf("%i",test());

cin.get();
return 0;
}

int test()
{

if (a[i][j]==*(&a[0][0] + 4*i + j))
return 1;
return 0;
}```

12. Yes. Try initializing your variables first.

Also, it looks like you're using an out-of-date compiler (if that actually compiles)
Code:
```#include <iostream>

int a[4][3] = { {1,2,3} , {4,5,6} , {7,8,9} ,  {10,11,12} };
int i=1,j=1;

int test();

int main(void)
{
std::cout<<test();
std::cin.get();

return 0;
}

int test()
{
if (a[i][j]==*(&a[0][0] + 4*i + j))
return 1;

return 0;
}```

13. Originally Posted by JaWiB
Yes. Try initializing your variables first.

Also, it looks like you're using an out-of-date compiler (if that actually compiles)
Code:
```#include <iostream>

int a[4][3] = { {1,2,3} , {4,5,6} , {7,8,9} ,  {10,11,12} };
int i=1,j=1;

int test();

int main(void)
{
std::cout<<test();
std::cin.get();

return 0;
}

int test()
{
if (a[i][j]==*(&a[0][0] + 4*i + j))
return 1;

return 0;
}```

actually i jus started picking up c++ a week ago and i am using DEV C++ to do the compiling. not sure which is gd for beginner.
i started programing with JAVA which i feel is easier to understand

14. almost

*(&a[0][0] + 4*i + j -1)

Which makes it a too complicated formula.
hmm... suppose i = j = 0.
*(&a[0][0] + 4*0 + 0 - 1) => *(&a[0][0] - 1)

This looks like one is trying to dereference a pointer that points to one before the first element.

Right away, this is dereferencing a memory address, which will make the program explode. It is wrong.
I think it is okay. This is from section 5.3.1 of the C++ Standard:
"The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is "pointer to T," the type of the result is "T.""
and a few lines later:
"The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualifiedid. In the first case, if the type of the expression is "T," the type of the result is "pointer to T.""

In this case a[0][0] is an lvalue, so &a[0][0] is valid. (&a[0][0] + 4*i + j) is just pointer arithmetic, and results in a pointer. Hence using unary operator* on it should be valid, assuming that the pointer is valid.

15. If you picked up C++ a week ago, why are you writing everything in C, anyway?