address of 2D array elements? 
Author Message
 address of 2D array elements?

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  
 address of 2D array elements?



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  
 address of 2D array elements?

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  
 address of 2D array elements?



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
you are talking about.

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  
 address of 2D array elements?

: 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  
 address of 2D array elements?

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  
 
 [ 6 post ] 

 Relevant Pages 

1. Which 2D array of 2D array addressing method?

2. 2D array of pointers to 2D arrays

3. scanf() w/ 2D-array address offsets: HELP!

4. Getting address of element of structure array.

5. making pointers to structure elements using array elements

6. Acces the 11th element of a 10 element array

7. checkout compilers (Was: Acces the 11th element of a 10 element array)

8. use 1d array + macro or 2d array?

9. Multiply and Add matrices with 2d array WITHOUT ARRAY INDEXING

10. How To pass 2D String array to VB from VC++ Using Safe array

11. Fast array transfer with 2D arrays...?

12. pointers to an array and accesing elements of the array

 

 
Powered by phpBB® Forum Software