Memory allocation in Structure to Structure

Discussion in 'C++' started by pra_ramli@rediffmail.com, Mar 8, 2006.

  1. Guest

    Hi all,
    While coding, I got struck up in memory allocation for structure to
    structure..
    For instance..I wrote the following pgm..

    #include <iostream.h>
    #include <stdlib.h>

    typedef struct A
    {
    int b;
    int c;
    struct E {
    int d;
    int e;
    } *E;
    } A;

    void main()
    {
    A* i = (A * ) calloc(1,sizeof(A);
    }

    I need to allocate memory for the internal structure E. Iam getting
    errors whatever format I follow.. i tried the following..
    A.E = (E * ) calloc(1,sizeof(E);
    A.E = (A.E * ) calloc(1,sizeof(E);
    and other combinations..

    Can anybody help me to sort out this problem???

    Cheers,
    Nick
    , Mar 8, 2006
    #1
    1. Advertising

  2. TB Guest

    skrev:
    > Hi all,
    > While coding, I got struck up in memory allocation for structure to
    > structure..
    > For instance..I wrote the following pgm..
    >
    > #include <iostream.h>
    > #include <stdlib.h>
    >
    > typedef struct A


    Are you writing C or C++?

    > {
    > int b;
    > int c;
    > struct E {
    > int d;
    > int e;
    > } *E;
    > } A;
    >


    struct A {
    struct E { };
    E * f;
    };

    > void main()
    > {
    > A* i = (A * ) calloc(1,sizeof(A);


    A * i = new A;
    i.f = new A::E;

    > }
    >
    > I need to allocate memory for the internal structure E. Iam getting
    > errors whatever format I follow.. i tried the following..
    > A.E = (E * ) calloc(1,sizeof(E);
    > A.E = (A.E * ) calloc(1,sizeof(E);
    > and other combinations..
    >
    > Can anybody help me to sort out this problem???


    Make sure the code is at least syntactically correct before you post.

    --
    TB @ SWEDEN
    TB, Mar 8, 2006
    #2
    1. Advertising

  3. Guest

    I modified to the code as following and now it is working
    #include <iostream.h>
    #include <stdlib.h>

    typedef struct A
    {
    int b;
    int c;
    struct E {
    int d;
    int e;
    } *F;
    }A;
    A b;
    void main()
    {


    b.F = new A::E; //mem alloc using new for a Nested Struct
    b.F = (A::E* ) calloc(1,sizeof(A)); //second way.c the declartion
    of struct


    }
    , Mar 9, 2006
    #3
    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. Replies:
    2
    Views:
    3,069
    Akshay
    Feb 16, 2005
  2. jvax

    memory allocation for structure array

    jvax, Jul 4, 2003, in forum: C Programming
    Replies:
    4
    Views:
    14,268
    siva2152
    Nov 24, 2010
  3. Trying_Harder

    Structure memory allocation

    Trying_Harder, Sep 16, 2003, in forum: C Programming
    Replies:
    4
    Views:
    7,778
    Irrwahn Grausewitz
    Sep 16, 2003
  4. Ken
    Replies:
    24
    Views:
    3,842
    Ben Bacarisse
    Nov 30, 2006
  5. chris
    Replies:
    6
    Views:
    974
    chris
    Oct 28, 2005
Loading...

Share This Page