Eric Boutin wrote:

Hi ! I would like to generate an array of type char[n][5];

I just dont really figure out how I could do it with malloc or calloc.. I

mean.. I know how to allocate a simple array with both of them; but when it

comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for char**

array = malloc(5*n*sizeof(char));, it'll return a void*, not a void**..

anyone have an idea ?

Output from new.c:

array[0][0] is 0

array[0][1] is 1

array[0][2] is 2

array[0][3] is 3

array[0][4] is 4

array[1][0] is 10

array[1][1] is 11

array[1][2] is 12

array[1][3] is 13

array[1][4] is 14

array[2][0] is 20

array[2][1] is 21

array[2][2] is 22

array[2][3] is 23

array[2][4] is 24

/* BEGIN new.c */

#include <stdio.h>

#include <stdlib.h>

#define N 3

int main(void)

{

size_t n, a, b;

char (*array)[5];

n = N;

array = malloc(n * sizeof *array);

if (!array) {

fputs("I'm tired\n", stderr);

exit(EXIT_FAILURE);

}

puts("Output from new.c:\n");

for (a = 0; a != n; ++a) {

for (b = 0; b != 5; ++b)

array[a][b] = (char)(10 * a + b);

}

for (a = 0; a != n; ++a) {

for (b = 0; b != 5; ++b) {

printf("array[%u][%u] is %u\n",

(unsigned)a,

(unsigned)b,

(unsigned)array[a][b]);

}

}

return 0;

}

/* END new.c */