Memory allocation in Structure to Structure

P

pra_ramli

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
 
T

TB

(e-mail address removed) 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.
 
P

pra_ramli

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


}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top