C/C++

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Allocating space for array of pointers?

    6 answers - 521 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    If I have this struct:
    #include <stdlib.h>
    #include <stdio.h>
    #define KIDS 4
    typedef struct test {
    int x;
    int y;
    } container;
    I would now like to make an array containing 4 pointers to this struct. But
    I would also like to allocate space for this array:
    int main(void)
    {
    container *new[4];
    new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERRR!
    return 0;
    }
    Do I have to allocate space for each pointer at a time?
  • No.1 | | 601 bytes | |

    Paminu wrote:
    If I have this struct:

    #include <stdlib.h>
    #include <stdio.h>
    #define KIDS 4

    typedef struct test {
    int x;
    int y;
    } container;
    --
    I would now like to make an array containing 4 pointers to this struct. But
    I would also like to allocate space for this array:

    int main(void)
    {
    container *new[4];
    new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERRR!

    return 0;
    }

    And what might that error be? (Looks K to me)

    Do I have to allocate space for each pointer at a time?

    No.

    HTH,
  • No.2 | | 1221 bytes | |

    Artie Gold wrote:

    Paminu wrote:
    >If I have this struct:
    >>

    >#include <stdlib.h>
    >#include <stdio.h>
    >#define KIDS 4
    >>

    >typedef struct test {
    >int x;
    >int y;
    >} container;
    >>
    >>

    >I would now like to make an array containing 4 pointers to this struct.
    >But I would also like to allocate space for this array:
    >>

    >int main(void)
    >{
    >container *new[4];
    >new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERRR!
    >>

    >return 0;
    >}
    >>

    >

    And what might that error be? (Looks K to me)

    test4.c:13: error: incompatible types in assignment

    where line 13 contains:
    new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERRR!

    I have instead tried:

    container *new[4];
    int j;
    for (j = 0; j < 4; j++)
    {
    new[j]=malloc(sizeof(container));
    new[j]=NULL;

    }

    Then I get no error!
  • No.3 | | 836 bytes | |

    Paminu wrote:
    If I have this struct:

    #include <stdlib.h>
    #include <stdio.h>
    #define KIDS 4

    typedef struct test {
    int x;
    int y;
    } container;
    --
    I would now like to make an array containing 4 pointers to this struct. But
    I would also like to allocate space for this array:

    int main(void)
    {
    container *new[4];
    new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERRR!

    As it should. You have already declared new as an array[4] of pointers
    to container.

    {
    size_t i;
    for (i = 0; i < 4; i++)
    if (!(new[i] = malloc(sizeof *new[i])))
    { /* handle error */ }
    }

    return 0;
    }

    Do I have to allocate space for each pointer at a time?

    No, you need to allocate space for what each pointer points to.

  • No.4 | | 901 bytes | |

    Artie Gold wrote:
    []
    container *new[4];
    new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERRR!
    []

    And what might that error be? (Looks K to me)

    "new" is an array of 4 pointers to "container", not a pointer to an
    array of 4 "container"s.

    Do I have to allocate space for each pointer at a time?

    No.

    It depends. If you want "new" to be an array of 4 pointers, then yes:

    container *new[4];/* As in the original code. */

    /* Allocate each pointer. This can also be in a for-loop */
    new[0] = malloc(sizeof(container));
    new[1] = malloc(sizeof(container));
    new[2] = malloc(sizeof(container));
    new[3] = malloc(sizeof(container));

    If you want it to point to an array of 4 containers:

    container *new;
    new = malloc(sizeof(container)*4);

    BTW, this is a perfect example of how C++ is not a "superset" of C.
  • No.5 | | 354 bytes | |

    Kenneth Brody wrote:
    Artie Gold wrote:
    []

    container *new[4];
    new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERRR!

    []
    >
    >>And what might that error be? (Looks K to me)

    >

    course, a ludicrous example of cerebral flatulence on m part
    [snip]
  • No.6 | | 531 bytes | |

    Thu, 09 Feb 2006 20:51:30 +0100, in comp.lang.c , Paminu
    <sdef@asd.comwrote:

    container *new[4];
    new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERRR!

    >
    >test4.c:13: error: incompatible types in assignment


    "new" is an array of four pointers. You can't assign directly to the
    array.

    >I have instead tried:

    new[j]=malloc(sizeof(container));

    yes, thats ok, because new[j] is a pointer.
    Mark McIntyre

Re: Allocating space for array of pointers?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 5 + 5 =
QUESTION ON "C/C++"

EMSDN.COM