how to dynamically allocate a structure containing pointer type structures? 
Author Message
 how to dynamically allocate a structure containing pointer type structures?

I have the following structure

struct NetworkModel{

struct LayerModel{

struct NeuronModel{
long double *W;
long double Y;
long double V;
long double Delta;
long double Error;
long double Treshold;

Quote:
}*N;
}*L;
}*nm;

Fairly simple :)
Each pointer type sub-element inside this structure is intended to
point an array of its king so I can use it like;
nm->L[a]->N[b]->W[c]=2;

To implement it I have the following idea which did not work
(something wrong fundemantally):

//I want to dynamically allocate the following NetworkModel
struct NetworkModel *ntw

//number of the LayerModel structures inside the NetworkModel to be x
ntw=(struct NetworkModel *)malloc(x*sizeof(struct LayerModel));

//and number of the NeuronModel structures inside the LayerModel to be
y
ntw.L=(struct LayerModel *)malloc(y*sizeof(struct NeuronModel));

//and finally I want 'W' inside the NeuronModel to be size of z
ntw.L.N=(struct NeuronModel *)malloc(z*sizeof(long double)+sizeof(long
double)*5);//here 5 is the rest of the doubles inside the NeuronModel

I appreciate your help.

Regards,
Sal.



Mon, 13 Jun 2005 06:44:38 GMT  
 how to dynamically allocate a structure containing pointer type structures?

Quote:

> I have the following structure

> struct NetworkModel{

> struct LayerModel{

> struct NeuronModel{
> long double *W;
> long double Y;
> long double V;
> long double Delta;
> long double Error;
> long double Treshold;
> }*N;

> }*L;

> }*nm;

> Fairly simple :)
> Each pointer type sub-element inside this structure is intended to
> point an array of its king so I can use it like;
> nm->L[a]->N[b]->W[c]=2;

No you can't.  After applying the subscripting operator you no longer
have a pointer, so -> is inappropriate.  What you can do is:

nm->L[a].N[b].W[c] = 2;

- Show quoted text -

Quote:
> To implement it I have the following idea which did not work
> (something wrong fundemantally):

> //I want to dynamically allocate the following NetworkModel
> struct NetworkModel *ntw

> //number of the LayerModel structures inside the NetworkModel to be x
> ntw=(struct NetworkModel *)malloc(x*sizeof(struct LayerModel));

> //and number of the NeuronModel structures inside the LayerModel to be
> y
> ntw.L=(struct LayerModel *)malloc(y*sizeof(struct NeuronModel));

> //and finally I want 'W' inside the NeuronModel to be size of z
> ntw.L.N=(struct NeuronModel *)malloc(z*sizeof(long double)+sizeof(long
> double)*5);//here 5 is the rest of the doubles inside the NeuronModel

You're assigning pointers to the wrong thing, and not initialising every
member of the dynamic arrays.  What you want is this:

#include <stdlib.h>

    struct NetworkModel *ntw;
    int i, j;

    ntw = malloc(sizeof *nm);
    ntw->L = malloc(x * sizeof *ntw->L);
    for (i = 0; i < x; i++) {
        ntw->L[i].N = malloc(y * sizeof *ntw->L[i].N);
        for (j = 0; j < y; j++) {
            ntw->L[i].N[j].W = malloc(z * sizeof *ntw->L[i].N[j].W);
        }
    }

        - Kevin.



Mon, 13 Jun 2005 09:00:26 GMT  
 how to dynamically allocate a structure containing pointer type structures?
Kevin, thank you very much
Quote:


> > I have the following structure

> > struct NetworkModel{

> > struct LayerModel{

> > struct NeuronModel{
> > long double *W;
> > long double Y;
> > long double V;
> > long double Delta;
> > long double Error;
> > long double Treshold;
> > }*N;

> > }*L;

> > }*nm;

> > Fairly simple :)
> > Each pointer type sub-element inside this structure is intended to
> > point an array of its king so I can use it like;
> > nm->L[a]->N[b]->W[c]=2;

> No you can't.  After applying the subscripting operator you no longer
> have a pointer, so -> is inappropriate.  What you can do is:

> nm->L[a].N[b].W[c] = 2;

> > To implement it I have the following idea which did not work
> > (something wrong fundemantally):

> > //I want to dynamically allocate the following NetworkModel
> > struct NetworkModel *ntw

> > //number of the LayerModel structures inside the NetworkModel to be x
> > ntw=(struct NetworkModel *)malloc(x*sizeof(struct LayerModel));

> > //and number of the NeuronModel structures inside the LayerModel to be
> > y
> > ntw.L=(struct LayerModel *)malloc(y*sizeof(struct NeuronModel));

> > //and finally I want 'W' inside the NeuronModel to be size of z
> > ntw.L.N=(struct NeuronModel *)malloc(z*sizeof(long double)+sizeof(long
> > double)*5);//here 5 is the rest of the doubles inside the NeuronModel

> You're assigning pointers to the wrong thing, and not initialising every
> member of the dynamic arrays.  What you want is this:

> #include <stdlib.h>

>     struct NetworkModel *ntw;
>     int i, j;

>     ntw = malloc(sizeof *nm);
>     ntw->L = malloc(x * sizeof *ntw->L);
>     for (i = 0; i < x; i++) {
>         ntw->L[i].N = malloc(y * sizeof *ntw->L[i].N);
>         for (j = 0; j < y; j++) {
>             ntw->L[i].N[j].W = malloc(z * sizeof *ntw->L[i].N[j].W);
>         }
>     }

>    - Kevin.



Wed, 15 Jun 2005 10:30:18 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Pointers to Structure that contains pointer to Function

2. dereferencing pointer to a structure containing a pointer to int

3. nested dynamically allocated structures

4. accessing dynamically allocated structures

5. Assign NULL to a dynamically allocated set of structures

6. Dynamically allocating a structure array

7. Passing Member of Dynamically Allocated Structure to Function

8. Marshaling a structure containing a aray of different structures

9. reading a structure containing pointers

10. structure containing pointer to char over the network

11. How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

12. self contained pointers to structures

 

 
Powered by phpBB® Forum Software