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]>
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]>