By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,445 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

Two-dimensional array to array of arrays

P: n/a
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[i][j] = i*10 + j;

for ( i=0 ; i < 10 ; i++ )
{
for ( j=0 ; j < 2 ; j++ )
printf("[%d][%d] = %2d ",i,j,array[i][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:Th*************@gmail.com>

Dec 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Kenneth Brody wrote:
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.

Dec 21 '06 #2

P: n/a
Kenneth Brody wrote:
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.

--
Simon.
Dec 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.