help generating an array of array with malloc or calloc

E

Eric Boutin

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 ?
 
P

pete

Eric said:
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] = (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]);
}
}
return 0;
}

/* END new.c */
 
N

Nejat AYDIN

Eric said:
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 ?

Read the C FAQ, Question 6.16
http://www.eskimo.com/~scs/C-faq/q6.16.html
 
J

Joe Wright

Eric said:
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 ?

You clearly need a better C book. K&R2 comes to mind. Chapter 5
pertains. There is also Steve Summit's C FAQ. Section 6 pertains. Read
more.
 
E

Eric Boutin

sorry I didn't read this point in the FAQ..

sorry

Nejat AYDIN said:
Eric said:
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 ?

Read the C FAQ, Question 6.16
http://www.eskimo.com/~scs/C-faq/q6.16.html
 
E

Eric Boutin

Thanks !

pete said:
Eric said:
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] = (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]);
}
}
return 0;
}

/* END new.c */
 
E

Eric Boutin

Thanks for book suggestion

Joe Wright said:
Eric said:
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 ?

You clearly need a better C book. K&R2 comes to mind. Chapter 5
pertains. There is also Steve Summit's C FAQ. Section 6 pertains. Read
more.
 
P

pete

Eric said:

You're welcome.
pete said:
Eric said:
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..
array = malloc(n * sizeof *array);

The way that this particular program was written,
it wasn't necessary to free(array),
but I believe that it's good to be in the habit of freeing
whatever is allocated, and I forgot to do that.

free(array);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top