I
Ioannis Vranos
Are the following guaranteed to work always as *C90* code?
1.
#include <stdio.h>
void some_func(int *p, const size_t SIZE)
{
size_t i;
for(i=0; i<SIZE; ++i)
printf("%d ", p);
}
int main(void)
{
int array[10][5]= {0};
some_func(array[0], sizeof(array)/sizeof(**array));
puts("");
return 0;
}
The above prints 50 zeros. I think it is guaranteed to work, since all
arrays are sequences of their elements.
2.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
size_t i, j;
int array[50]= {0};
int (*p)[5]= (int (*)[5])(&array[0]);
for (i= 0; i< 10; ++i)
for(j=0; j<5; ++j)
printf("%d ", p[j]);
puts("");
return 0;
}
Here p behaves as a 2-dimensional matrix, that is a 10x5 matrix. I think
it is guaranteed to work for the same reason as the first one, that is
we can treat an array (sequence) of integers as various types of integer
arrays.
1.
#include <stdio.h>
void some_func(int *p, const size_t SIZE)
{
size_t i;
for(i=0; i<SIZE; ++i)
printf("%d ", p);
}
int main(void)
{
int array[10][5]= {0};
some_func(array[0], sizeof(array)/sizeof(**array));
puts("");
return 0;
}
The above prints 50 zeros. I think it is guaranteed to work, since all
arrays are sequences of their elements.
2.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
size_t i, j;
int array[50]= {0};
int (*p)[5]= (int (*)[5])(&array[0]);
for (i= 0; i< 10; ++i)
for(j=0; j<5; ++j)
printf("%d ", p[j]);
puts("");
return 0;
}
Here p behaves as a 2-dimensional matrix, that is a 10x5 matrix. I think
it is guaranteed to work for the same reason as the first one, that is
we can treat an array (sequence) of integers as various types of integer
arrays.