B
Boon
Hello everyone,
Consider a "set" of M=N*N integers. In some parts of my program, I want
to handle this set as a 1D array of M integers. In other parts of my
program, I want to handle this set as a 2D matrix of N*N integers.
Is the following code 100% legal C89 code ?
#include <assert.h>
#define N 9
typedef int row_t[N];
void compare(int *array)
{
int i, j;
row_t *matrix = (row_t *)array;
for (i=0; i < N; ++i)
for (j=0; j < N; ++j)
{
assert( &matrix[j] == &array[i*N+j] );
assert( matrix[j] == array[i*N+j] );
}
}
int main(void)
{
int i;
int foo[N*N];
for (i=0; i < N*N; ++i) foo = 7*i+11;
compare(foo);
return 0;
}
Can I be sure that the asserts will never fail ?
Regards.
Consider a "set" of M=N*N integers. In some parts of my program, I want
to handle this set as a 1D array of M integers. In other parts of my
program, I want to handle this set as a 2D matrix of N*N integers.
Is the following code 100% legal C89 code ?
#include <assert.h>
#define N 9
typedef int row_t[N];
void compare(int *array)
{
int i, j;
row_t *matrix = (row_t *)array;
for (i=0; i < N; ++i)
for (j=0; j < N; ++j)
{
assert( &matrix[j] == &array[i*N+j] );
assert( matrix[j] == array[i*N+j] );
}
}
int main(void)
{
int i;
int foo[N*N];
for (i=0; i < N*N; ++i) foo = 7*i+11;
compare(foo);
return 0;
}
Can I be sure that the asserts will never fail ?
Regards.