help generating an array of array with malloc or calloc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Boutin

    help generating an array of array with malloc or calloc

    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*size of(char));, it'll return a void*, not a void**..
    anyone have an idea ?


  • pete

    #2
    Re: help generating an array of array with malloc or calloc

    Eric Boutin wrote:[color=blue]
    >
    > 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*size of(char));, it'll return a void*, not a void**..
    > anyone have an idea ?[/color]

    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_FAILU RE);
    }
    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 */

    Comment

    • Nejat AYDIN

      #3
      Re: help generating an array of array with malloc or calloc

      Eric Boutin wrote:[color=blue]
      >
      > 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*size of(char));, it'll return a void*, not a void**..
      > anyone have an idea ?[/color]

      Read the C FAQ, Question 6.16

      Comment

      • Joe Wright

        #4
        Re: help generating an array of array with malloc or calloc

        Eric Boutin wrote:[color=blue]
        >
        > 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*size of(char));, it'll return a void*, not a void**..
        > anyone have an idea ?[/color]

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

        Comment

        • Eric Boutin

          #5
          Re: help generating an array of array with malloc or calloc

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

          sorry

          "Nejat AYDIN" <nejataydin@sup eronline.com> a écrit dans le message de
          news:3FC9FFA3.6 5BA405C@superon line.com...[color=blue]
          > Eric Boutin wrote:[color=green]
          > >
          > > 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..[/color][/color]
          I[color=blue][color=green]
          > > mean.. I know how to allocate a simple array with both of them; but[/color][/color]
          when it[color=blue][color=green]
          > > comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for[/color][/color]
          char**[color=blue][color=green]
          > > array = malloc(5*n*size of(char));, it'll return a void*, not a void**..
          > > anyone have an idea ?[/color]
          >
          > Read the C FAQ, Question 6.16
          > http://www.eskimo.com/~scs/C-faq/q6.16.html[/color]


          Comment

          • Eric Boutin

            #6
            Re: help generating an array of array with malloc or calloc

            Thanks !

            "pete" <pfiland@mindsp ring.com> a écrit dans le message de
            news:3FC9FE88.D 2D@mindspring.c om...[color=blue]
            > Eric Boutin wrote:[color=green]
            > >
            > > 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..[/color][/color]
            I[color=blue][color=green]
            > > mean.. I know how to allocate a simple array with both of them; but[/color][/color]
            when it[color=blue][color=green]
            > > comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for[/color][/color]
            char**[color=blue][color=green]
            > > array = malloc(5*n*size of(char));, it'll return a void*, not a void**..
            > > anyone have an idea ?[/color]
            >
            > 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_FAILU RE);
            > }
            > 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 */[/color]


            Comment

            • Eric Boutin

              #7
              Re: help generating an array of array with malloc or calloc

              Thanks for book suggestion

              "Joe Wright" <joewwright@ear thlink.net> a écrit dans le message de
              news:3FCA1593.5 BD@earthlink.ne t...[color=blue]
              > Eric Boutin wrote:[color=green]
              > >
              > > 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..[/color][/color]
              I[color=blue][color=green]
              > > mean.. I know how to allocate a simple array with both of them; but[/color][/color]
              when it[color=blue][color=green]
              > > comes to a 2 dimension array.. I'm stuck.. I mean.. if I ask for[/color][/color]
              char**[color=blue][color=green]
              > > array = malloc(5*n*size of(char));, it'll return a void*, not a void**..
              > > anyone have an idea ?[/color]
              >
              > 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 ---[/color]


              Comment

              • pete

                #8
                Re: help generating an array of array with malloc or calloc

                Eric Boutin wrote:[color=blue]
                >
                > Thanks ![/color]

                You're welcome.
                [color=blue]
                > "pete" <pfiland@mindsp ring.com> a écrit dans le message de
                > news:3FC9FE88.D 2D@mindspring.c om...[color=green]
                > > Eric Boutin wrote:[color=darkred]
                > > >
                > > > 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..[/color][/color][/color]
                [color=blue][color=green]
                > > array = malloc(n * sizeof *array);[/color][/color]

                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.
                [color=blue]
                >[color=green]
                > > }[/color][/color]

                free(array);
                [color=blue][color=green]
                > > return 0;
                > > }
                > >
                > > /* END new.c */[/color][/color]


                --
                pete

                Comment

                Working...