C/C++

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

    3 answers - 482 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:
    typedef struct test{
    int x;
    int y;
    }container;
    Now I would like to make an array of 5 pointers to this struct:
    int main(void){
    container *cp[5];
    cp=malloc(sizeof(container));
    container->content="big";
    container->tt=malloc(sizeof(container *)*5); //THIS LINE GIVES ERRR!
    return 0;
    }
    But how do I allocate space for these pointers? Do I have to do it for each
    one at a time?
  • No.1 | | 1236 bytes | |

    Paminu wrote:
    If I have this struct:

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

    Now I would like to make an array of 5 pointers to this struct:

    int main(void){
    container *cp[5];
    cp=malloc(sizeof(container));
    container->content="big";
    container->tt=malloc(sizeof(container *)*5); //THIS LINE GIVES ERRR!

    return 0;
    --
    }

    But how do I allocate space for these pointers? Do I have to do it for each
    one at a time?
    You wrote container *cp[5]. That means you already allocated space
    for 5 pointers to container objects. cp[0]cp[4] are all pointers
    pointing who knows where Now, if you want to allocate space
    for a container object and make cp[3] point to it, you could do:
    cp[3]=malloc(sizeof(container));
    and
    cp[3]->x=something;
    cp[3]->y=something_else;
    use free(cp[3]) to free the allocated space that cp[3] points to.

    If you have a global variable:
    container gcp;
    you can say cp[3]=&gcp;
    You should not free the pointer as you haven't allocated space,
    the program did. Also, pay attention if you do that with auto
    (local) variables as they are destroyed when the function, they
    are defined in, ends.
  • No.2 | | 1696 bytes | |

    Nelu wrote:

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

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

    >Now I would like to make an array of 5 pointers to this struct:
    >>

    >int main(void){
    >container *cp[5];
    >cp=malloc(sizeof(container));
    >container->content="big";
    >container->tt=malloc(sizeof(container *)*5); //THIS LINE GIVES
    >ERRR!
    >>

    >return 0;
    >>
    >>

    >}
    >>

    >But how do I allocate space for these pointers? Do I have to do it for
    >each one at a time?

    You wrote container *cp[5]. That means you already allocated space
    for 5 pointers to container objects. cp[0]cp[4] are all pointers
    pointing who knows where Now, if you want to allocate space
    for a container object and make cp[3] point to it, you could do:
    cp[3]=malloc(sizeof(container));
    and
    cp[3]->x=something;
    cp[3]->y=something_else;
    use free(cp[3]) to free the allocated space that cp[3] points to.

    If you have a global variable:
    container gcp;
    you can say cp[3]=&gcp;
    You should not free the pointer as you haven't allocated space,
    the program did. Also, pay attention if you do that with auto
    (local) variables as they are destroyed when the function, they
    are defined in, ends.

    sorry tried to cancel this message as soon as possible, because I was
    writing another more specfic one
  • No.3 | | 1546 bytes | |

    Nelu wrote:

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

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

    >Now I would like to make an array of 5 pointers to this struct:
    >>

    >int main(void){
    >container *cp[5];
    >cp=malloc(sizeof(container));
    >container->content="big";
    >container->tt=malloc(sizeof(container *)*5); //THIS LINE GIVES
    >ERRR!
    >>

    >return 0;
    >>
    >>

    >}
    >>

    >But how do I allocate space for these pointers? Do I have to do it for
    >each one at a time?

    You wrote container *cp[5]. That means you already allocated space
    for 5 pointers to container objects. cp[0]cp[4] are all pointers
    pointing who knows where Now, if you want to allocate space
    for a container object and make cp[3] point to it, you could do:
    cp[3]=malloc(sizeof(container));
    and
    cp[3]->x=something;
    cp[3]->y=something_else;

    But what if I would like all the pointers: cp[0]cp[0] to point to NULL?
    Do I need a for loop or is it possible to it with one commando?

    The same thing when I want to allocate space for the object that the
    pointers point to, I can do it in a for loop but would like to know if I
    could do it with malloc only one time.

Re: Allocating space for array of pointers?


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

EMSDN.COM