C examples for Array:Multidimensional Arrays
You must declare a pointer that specifies all but the leftmost array dimension.
The following code builds a table of the numbers 1 through 10 raised to their first, second, third, and fourth powers.
#include <stdio.h> #include <stdlib.h> int pwr(int a, int b); int main(void) { /* Declare a pointer to an array that has 10 ints in each row. *///w ww . j a v a2s . c om int* p; register int i, j; /* allocate memory to hold a 4 x 10 array */ p = (int*)malloc(40 * sizeof(int)); if (!p) { printf("Memory request failed.\n"); exit(1); } for (j = 1; j<11; j++) for (i = 1; i<5; i++) p[i - 1] = pwr(j, i); for (j = 1; j<11; j++) { for (i = 1; i<5; i++) printf("%10d ", p[i - 1]); printf("\n"); } return 0; } /* Raise an integer to the specified power. */ int pwr(int a, int b) { register int t = 1; for (; b; b--) t = t*a; return t; }