code said:
Hi all,
i come across the following declaration in some code.
int (*ptr)[ 10];
any feedback helps a lot for me.
It's a pointer to an array of ten int.
It can be used to access a two dimensional array
through a single pointer.
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#define X 2
#define Y 10
int main(void)
{
int (*ptr)[Y];
int x, y;
ptr = malloc(X * sizeof *ptr);
if (ptr == NULL) {
puts("ptr == NULL");
exit(EXIT_FAILURE);
}
for (x = 0; x != X; ++x) {
for (y = 0; y != Y; ++y) {
ptr[x][y] = Y * x + y;
}
}
for (x = 0; x != X; ++x) {
for (y = 0; y != Y; ++y) {
printf("%2d ", ptr[x][y]);
}
putchar('\n');
}
free(ptr);
return 0;
}
/* END new.c */