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

Printable View

- 06-03-2006esadakohelp 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 - 06-03-2006Mario F.
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 - 06-03-2006pianorainQuote:

Originally Posted by**Mario F.**

A multidimensional array isn't usually a single dimension array. Consider argv. Each argument is a different length. - 06-03-2006Mario F.
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};

The second definition helps making it clear which answer is the right one. - 06-03-2006whiteflags
> 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. - 06-03-2006Mario F.
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 - 06-03-2006esadako
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?? - 06-03-2006Mario F.
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 - 06-03-2006whiteflags
> 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. - 06-03-2006laserlightQuote:

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.

- 06-03-2006esadako
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;

}

- 06-03-2006JaWiB
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;

}

- 06-03-2006esadakoQuote:

Originally Posted by**JaWiB**

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 - 06-03-2006laserlightQuote:

almost

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

Which makes it a too complicated formula.

*(&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.

Quote:

Right away, this is dereferencing a memory address, which will make the program explode. It is wrong.

"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. - 06-03-2006whiteflags
If you picked up C++ a week ago, why are you writing everything in C, anyway?