Home > Net >  Pointers to a struct inside a struct
Pointers to a struct inside a struct

Time:08-07

I have two structures (one_d and two_d). I have a function that will take struct two_d *smg as input. In main(), I am trying to create such smg so it will return value c increased.

My problem is that, while creating an array of struct two_d smg[2], I am not sure how to put inside information about its values, as it is a pointer to a different struct.

So how do you use pointer to a struct inside a struct? I would like to create struct two_d smg[2] but i dont now how to deal with struct one_d *a field in it

#include <stdio.h>

enum sid 
{

DRB,
DRA,
};



struct one_d
{
unsigned int r;
unsigned int *p;
};


struct two_d
{
struct one_d *a;
enum sid z;
};


unsigned int getSmg(struct two_d *smg)
{
unsigned int c = 0;  
const struct two_d *sd = NULL;
const struct one_d *ed = NULL;


for (sd = smg; sd->a != NULL;   sd)
{
   
    for (ed = sd->a; ed->p != NULL;   ed)
    {
      
        if (DRA == sd->z)
        {
            /*P Increment the clear-state buffer size */
            c  = 1   ed->r;
        }
     
    }
    
}


return c;
}





int main(void)
{
unsigned int rVal = 0;

struct two_d smg[2]={
//
//  [0].a ={1,0},
//  [0].z =DRA,
//  [1].a={1,0},
// [1].z =DRA,
};

rVal = getSmg(smg);

printf("Return value is a %d\n", rVal);

printf("Return value is a l");
return( 0 );
}

CodePudding user response:

Well, at least this compiles... I'm not game to run it, though...

For what it's worth...

enum sid { DRB, DRA, DRAwhoCares };

typedef struct {
    unsigned int r;
    unsigned int *p;
} oneD_t;

typedef struct {
    oneD_t *a;
    enum sid z;
} twoD_t;

unsigned int getSmg( twoD_t *smg ) {
    unsigned int c = 0;  

    for( twoD_t *sd = smg; sd->a != NULL;  sd   ) {
        for( oneD_t *ed = sd->a; ed->p != NULL; ed   ) {
            if( DRA == sd->z ) {
                /*P Increment the clear-state buffer size */
                c  = 1   ed->r;
            }
        }
    }
    return c;
}

int main( void ) {

    oneD_t foo[] = { { 1, NULL }, /* ... */ };
    oneD_t bar[] = { { 1, NULL }, /* ... */ };

    twoD_t smg[]={
        { foo, DRA, },
        { bar, DRA, },
        { NULL, DRAwhoCares, },
    };

    unsigned int rVal = getSmg( smg );

    printf( "Return value: %u\n", rVal );

    return 0; // return is not a function call... No parenthesis...
}
  •  Tags:  
  • c
  • Related