Author Message

in one dimension array a[6], the value a[i] is stored in address a+i

What happen in multi dimension array
for example a[2][3]?

Expecting the same as for a[i], I have tested but found that a memory of
2 bytes was allocated for each a[i][j] which is different from 4 bytes
for a[i]. Furthermore, the value when printed out is not as assigned.

Could anyone please tell me if there is a way to refer to 2D array
elements through increment the origin pointer

Thank you very much.

Mon, 23 Oct 2000 03:00:00 GMT

Quote:
> in one dimension array a[6], the value a[i] is stored in address a+i

> What happen in multi dimension array
> for example a[2][3]?

> Expecting the same as for a[i], I have tested but found that a memory of
> 2 bytes was allocated for each a[i][j] which is different from 4 bytes
> for a[i]. Furthermore, the value when printed out is not as assigned.

> Could anyone please tell me if there is a way to refer to 2D array
> elements through increment the origin pointer

> Thank you very much.

This is how it works

ptr=&a[0][0]

ptr now points to the start of the array.

ptr     now points to   a[0][0]
ptr+1                       a[0][1]
ptr+2                       a[0][2]
ptr+3                       a[1][0]
ptr+4                       a[1][1]

etc...

Mon, 23 Oct 2000 03:00:00 GMT

Quote:

> in one dimension array a[6], the value a[i] is stored in address a+i

Hi QTPHA2,

Actually the compiler translates "a[i]" into "*(a+i)". This translation
is responsible for the following obscure thing in the comp.lang.c FAQ:
6.11:   I came across some "joke" code containing the "expression"
5["abcdef"] .  How can this be legal C?

Quote:
> What happen in multi dimension array
> for example a[2][3]?

If it is a real multidimensional array, like "inta[SIZE_Y][SIZE_X];" the
translation depends on the dimesion(s) of the array. The only dimension
that is not needed for the translation is the rightmost one. The
expression "a[y][x]" is translated into "*(a + y * SIZE_Y + x)".

Quote:
> Expecting the same as for a[i], I have tested but found that a memory of
> 2 bytes was allocated for each a[i][j] which is different from 4 bytes
> for a[i]. Furthermore, the value when printed out is not as assigned.

You should maybe post a code sample to illustrate your problem. If you
are talking about a 2D array "a", the expression "a[i]" yields a
*pointer* (the address of row i), whereas "a[i][j]" yields the array
element at position (i,j). The differing sizes in bytes result from
your compiler using different byte sizes for a pointer and your array
element data type.

The comp.lang.c FAQ contains a very good explanation of the problems
and interesting effects that occur in the context of arrays and
pointers in the section:
Section 6.  Arrays and Pointers

You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
posted to this newsgroup and to news.answers regularly (at the
beginning of each month).

Stephan
(initiator of the campaign against grumpiness in c.l.c)

Mon, 23 Oct 2000 03:00:00 GMT

Quote:

>> in one dimension array a[6], the value a[i] is stored in address a+i

>> What happen in multi dimension array
>> for example a[2][3]?

What is then element type of the array?

Quote:
>> Expecting the same as for a[i], I have tested but found that a memory of
>> 2 bytes was allocated for each a[i][j] which is different from 4 bytes
>> for a[i]. Furthermore, the value when printed out is not as assigned.

This doesn't make much sense. Post an example program to demonstrate what

Quote:
>> Could anyone please tell me if there is a way to refer to 2D array
>> elements through increment the origin pointer

>> Thank you very much.

>This is how it works

>ptr=&a[0][0]

For that to work, given that a is defined as:

type a[2][3];

then ptr would be defined as:

type (*ptr)[3];

i.e. a pointer to a array of 3 elements of type.

Quote:
>ptr now points to the start of the array.

Right.

Quote:

>ptr     now points to   a[0][0]

No, ptr points to a[0]

Quote:
>ptr+1                       a[0][1]

a[1]

Quote:
>ptr+2                       a[0][2]

a[2]

Quote:
>ptr+3                       a[1][0]

a[3]

Quote:
>ptr+4                       a[1][1]

a[4]

To get your results you would have to write something like:

type *ptr = (type *)a;

--
-----------------------------------------

-----------------------------------------

Mon, 23 Oct 2000 03:00:00 GMT

: in one dimension array a[6], the value a[i] is stored in address a+i

: What happen in multi dimension array
: for example a[2][3]?

: Expecting the same as for a[i], I have tested but found that a memory of
: 2 bytes was allocated for each a[i][j] which is different from 4 bytes
: for a[i]. Furthermore, the value when printed out is not as assigned.

: Could anyone please tell me if there is a way to refer to 2D array
: elements through increment the origin pointer

It sounds as if you are on a system in which a pointer, like a[i], is
four bytes and the elements of array a, such as a[i][j], are two bytes.
Try posting some code which shows your problem.

Will

Mon, 23 Oct 2000 03:00:00 GMT

If you have the following definition

int a[ 6 ], b[ 2 ][ 3 ];

The allocated memory for each element a[ i ] and b[ i ][ j ] must be the
same because they are of the same type.  If an int is 4 bytes in your
operating system, then each element in each of the preceding arrays has to
be 4 bytes.  How did you come to the conclusion that they are different?

If you want to step through array b using a single index, consider something
like this

int *ptr, i;
ptr = (int *)b;
for ( i = 0; i < sizeof (b) / sizeof (int); ++i )
{
DoSomething( ptr[ i ] );

Quote:
}

--
Increase the Peace!
Charles LaCour

Quote:

>in one dimension array a[6], the value a[i] is stored in address a+i

>What happen in multi dimension array
>for example a[2][3]?

>Expecting the same as for a[i], I have tested but found that a memory of
>2 bytes was allocated for each a[i][j] which is different from 4 bytes
>for a[i]. Furthermore, the value when printed out is not as assigned.

>Could anyone please tell me if there is a way to refer to 2D array
>elements through increment the origin pointer

>Thank you very much.

Mon, 23 Oct 2000 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages