help generating an array of array with malloc or calloc

Discussion in 'C Programming' started by Eric Boutin, Nov 30, 2003.

  1. Eric Boutin

    Eric Boutin Guest

    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 ?
     
    Eric Boutin, Nov 30, 2003
    #1
    1. Advertising

  2. Eric Boutin

    pete Guest

    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] = (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 */
     
    pete, Nov 30, 2003
    #2
    1. Advertising

  3. Eric Boutin

    Nejat AYDIN Guest

    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 ?


    Read the C FAQ, Question 6.16
    http://www.eskimo.com/~scs/C-faq/q6.16.html
     
    Nejat AYDIN, Nov 30, 2003
    #3
  4. Eric Boutin

    Joe Wright Guest

    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 ?


    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.
    --
    Joe Wright http://www.jw-wright.com
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Nov 30, 2003
    #4
  5. Eric Boutin

    Eric Boutin Guest

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

    sorry

    "Nejat AYDIN" <> a écrit dans le message de
    news:...
    > 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 ?

    >
    > Read the C FAQ, Question 6.16
    > http://www.eskimo.com/~scs/C-faq/q6.16.html
     
    Eric Boutin, Nov 30, 2003
    #5
  6. Eric Boutin

    Eric Boutin Guest

    Thanks !

    "pete" <> a écrit dans le message de
    news:...
    > 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] = (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 */
     
    Eric Boutin, Nov 30, 2003
    #6
  7. Eric Boutin

    Eric Boutin Guest

    Thanks for book suggestion

    "Joe Wright" <> a écrit dans le message de
    news:...
    > 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 ?

    >
    > 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.
    > --
    > Joe Wright http://www.jw-wright.com
    > "Everything should be made as simple as possible, but not simpler."
    > --- Albert Einstein ---
     
    Eric Boutin, Nov 30, 2003
    #7
  8. Eric Boutin

    pete Guest

    Eric Boutin wrote:
    >
    > Thanks !


    You're welcome.

    > "pete" <> a écrit dans le message de
    > news:...
    > > 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..


    > > 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);

    > > return 0;
    > > }
    > >
    > > /* END new.c */



    --
    pete
     
    pete, Dec 1, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. luca
    Replies:
    5
    Views:
    27,240
    Default User
    Mar 2, 2004
  2. Jun Woong

    Re: Override malloc,calloc,realloc and free?

    Jun Woong, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,099
    Jun Woong
    Jun 26, 2003
  3. Dan Pop
    Replies:
    0
    Views:
    911
    Dan Pop
    Jun 26, 2003
  4. Douglas A. Gwyn

    Re: Override malloc,calloc,realloc and free?

    Douglas A. Gwyn, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    759
    Douglas A. Gwyn
    Jun 26, 2003
  5. MK
    Replies:
    27
    Views:
    2,757
    Sidney Cadot
    Feb 13, 2004
Loading...

Share This Page