user8506163
user8506163

Reputation:

Sending Pointers of Structs to a Function in C

I don't love this, but I have a struct with nearly 45 members inside; all are characters or character arrays. That said, I am going to need to optimize the way I initialize each struct. Ordinarily, I would pass the entire object into my init_struct() function, but I feel like that is not the best way to do this.

How would I create and use a pointer to the struct to accomplish this?

Old Method would look something like this:

void init_struct(struct general){
...initialize members...
}

int main(){
  struct general[10];

  for(int i = 0 ; i < 10 ; ++i){
     init_struct(general[i];
  }

}

Since this struct is so large, as I said nearly 45 members inside it, I think a point to the struct would go a long way in optimizing this process. How would I accomplish that?


Just in case you need, here is the typedef for my struct

typedef struct
{ 
  //Basically, everything we want to read from HUDL should be here...
  int play_num;
  char down;
  char dist[3];
  char ydln[4];
  char gnls[3];
  char hash[3];
  char home[20];
  char away[20];
  char odk[2];
  char qtr[2];
  char series[3];
  char result[20];
  char penalty[20];

  char act_cb[20]; //How do they act post-snap
  char act_dl[20];
  char act_lb[20];
  char act_ol[20];
  char act_qb[20];
  char act_rb[20];
  char act_saf[20];

  char aln_cb[20]; //How do they align pre-snap
  char aln_dl[20];
  char aln_lb[20];
  char aln_ol[20];
  char aln_qb[20];
  char aln_rb[20];
  char aln_saf[20];
  char aln_wr[20];

  char blitz[20];
  char box_cnt[3];
  char saf_count[20];
  char coverage[20];
  char cvr_basic[20];
  char def_front[20]; 
  char mtn_def[20];
  char num_rush[3];

  char off_form[20];
  char form_var[20];
  char motion[20];
  char off_pro[20];
  char off_play[20];
  char play_var[20];
  char personnel[20];
  char play_type[20];


  char time[2]; 
  char score_diff[4];
  char field_zone[2];
  char dd_type[2];
  char form_strength[2];

} HUDL; // MAXIMUM of 63 Members

Upvotes: 1

Views: 191

Answers (3)

Vivek Vijayan
Vivek Vijayan

Reputation: 337

A slightly cleaner and better approach would be to have a constructor and destructor function to allocate memory dynamically to structure and free it after use.

static void HUDL_destroy(HUDL* ptr)
{
  if(ptr)
  {
    //...any other clean up that needs to be done goes here..
    free(ptr);
  }
}

static HUDL* HUDL_create()
{
  HUDL* ptr = malloc(sizeof(HUDL));

  if(!ptr)
    return NULL;

  //do initialization bits...
  init_struct(ptr);

  return ptr;
}

int main()
{
  //allocate and initialise structure
  HUDL *general = HUDL_create();

  //do stuff...

  //free structure after use
  HUDL_destroy(general);

}

You might need an array of pointers in your case. So modify your main() accordingly.

int main()
{
  //we need an array of structure pointers
  HUDL* general[SIZE];

  //allocate and initialize structure
  for(int i=0; i<SIZE; i++)
    general[i] = HUDL_create();

  //do stuff...

  //free structure after use
  for(i=0; i<SIZE; i++)
    HUDL_destroy( general[i] );
}

Upvotes: 0

savram
savram

Reputation: 580

There's a couple of things wrong on your code. First of, your function definition is wrong because you omit the parameter name. Your function definition should look like this:

void init_struct(struct general mygeneralstruct){}

Alternatively, you could use an alias for your struct using typedef, like so:

typedef struct {
  int a;
} general;

In which case, your function declaration could look like this:

void init_struct(general mygeneralstruct){}

You have the same problem when you declare your array of structures. You omit the name of your variable. Instead of

struct general[10];

it should be

struct general mygeneralstruct[10]

or general mygeneralstruct[10](typedef)

Finally, you can't change your array of structures by passing each structure's value to the function. You need to pass each structure's address instead. Your function declaration should then be(using typedef):

void init_struct(general* mygeneralstruct){}

and the code in the loop:

init_struct(&mygeneralstruct[i]);

Upvotes: 1

SPlatten
SPlatten

Reputation: 5762

To pass a pointer to your array element, you just prefix the parameter with &, make sure you declare the function correctly:

    void init_struct(HUDL* pGeneral){
        if ( pGeneral != NULL ) {
    //This will ensure the entire structure contains '0'
           memset(pGeneral, 0, sizeof(HUDL));
    ...initialize members...
        }
    }

    int main(){
        HUDL general[10];

        for( int i=0; i<(sizeof(general) / sizeof(general[0])); i++ ) {
            init_struct(&general[i]);
        }
    }

I'm not sure why you haven't used the typedef 'HUDL' makes life a lost easier and code easier to read.

Upvotes: 0

Related Questions