1

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
Vivek Vijayan
  • 337
  • 5
  • 19
  • 2
    Read just about any [good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). They should have chapters on how to use pointers, including pointers to structures. – Some programmer dude Sep 05 '17 at 13:06
  • See answer by SPlatten and note the use of pointers (`*`) and address of (`&`) operators. And read book. – Paul Ogilvie Sep 05 '17 at 13:11
  • 1
    You old function, i.e. `void init_struct(struct general){` is wrong and can't have been the one used – Support Ukraine Sep 05 '17 at 13:26
  • The way you name and group members indicates that there might be some smaller structs trying to get out. It's not at all unusual to start with some small structs and then use them to build the next level. And unless `ydln` and `gnls` are well known terms in the problem domain, I would try to use some names that are more easy to read. – Bo Persson Sep 05 '17 at 13:44
  • Read about pointers to structures and array of pointers. – Vivek Vijayan Sep 05 '17 at 14:34
  • Do you have any working code? Post a [mcve] then. We don't need all 300 of the struct members, 3 is enough for a demo. We then can discuss what's wrong with your current approach. As of now there isn't any approach to be seen, just a bunch of lines vaguely resembling C code. – n. m. could be an AI Sep 05 '17 at 15:53

3 Answers3

1

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]);
savram
  • 500
  • 4
  • 18
0

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.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
0

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] );
}
Vivek Vijayan
  • 337
  • 5
  • 19