Two-dimensional array to array of arrays

K

Kenneth Brody

Given the following current definition:

int array[100][2];

Is the following equivalent?

(By "equivalent", I mean that any reference to "array[x][y]" retains
the same meaning, and that the memory layout of the 200 ints is
identical.)

typedef int TwoInts[2];
TwoInts array[100];

What about:

TwoInts *array = malloc( 100 * sizeof(*array) );

Yes, I realize that type actual type of "array" has changed, and that
malloc() may fail, and so on.

I just need to verify that "array[x][y]" hasn't changed meaning. A
quick test program seems to confirm that the behavior has not changed,
but I know that implementation-defined behavior may "work" on one
platform an not another. I'm pretty sure that the behavior here is
defined by the standard, and not implementation-defined, but I'd
like confirmation.

Thanks.

==========
#include <stdio.h>

typedef int TwoInts[2];
int array[10][2];

int main(int argc,char *argv[])
{
int i,j;
TwoInts *x;

for ( i=0 ; i < 10 ; i++ )
for ( j=0 ; j < 2 ; j++ )
array[j] = i*10 + j;

for ( i=0 ; i < 10 ; i++ )
{
for ( j=0 ; j < 2 ; j++ )
printf("[%d][%d] = %2d ",i,j,array[j]);
printf("\n");
}

printf("\n");
x = array;

for ( i=0 ; i < 10 ; i++, x++ )
{
for ( j=0 ; j < 2 ; j++ )
printf("[%d][%d] = %2d ",i,j,(*x)[j]);
printf("\n");
}

return(0);
}
==========

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

mark_bluemel

Kenneth said:
Given the following current definition:

int array[100][2];

array is an array of 100 items, each of which is an array of 2 ints.
Is the following equivalent?
typedef int TwoInts[2];
TwoInts array[100];

array is an array of 100 items, each of which is an array of 2 ints.
What about:

TwoInts *array = malloc( 100 * sizeof(*array) );

array is a pointer to 100 items, each of which is an array of 2 ints.
It can be handled as an array.

That's my interpretation.
 
S

Simon Biber

Kenneth said:
Given the following current definition:

int array[100][2];

array has type int[100][2], which decays to an int(*)[2] pointing at the
first two-int element of an array of 100 such elements.
Is the following equivalent?

(By "equivalent", I mean that any reference to "array[x][y]" retains
the same meaning, and that the memory layout of the 200 ints is
identical.)

typedef int TwoInts[2];
TwoInts array[100];

array has the same type int[100][2], which of course decays in the same way.
What about:

TwoInts *array = malloc( 100 * sizeof(*array) );

array is an int(*)[2] and, assuming malloc succeeded, points at the
first two-int element of an array of 100 such elements.

They decay to the same thing, when used in any other way apart from as
the operand of unary & or sizeof.

It may be used identically to the first two, except as an operand to
unary & or sizeof.

In the first two cases case &array is int(*)[100][2]
In the last case &array is int(**)[2]

In the first two cases sizeof array is 100*2*sizeof(int)
In the last case, sizeof array is sizeof(int(*)[2]) which is usually the
same size as any other object pointer: 2, 4 or 8 bytes are typical.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top