NO

Author Topic: Passing arrays of structures...  (Read 2276 times)

PhilG57

  • Guest
Passing arrays of structures...
« on: December 12, 2014, 03:45:43 AM »
Grrr.  I'm feeling not so smart as I cannot for the life of me figure this out...

I would like to call a subroutine and have it return to me an initialized, contigous group of structures, each sructure and each element addressable using a subscript in both the calling and called routines.  The idea is to call this subroutine any time I need a new chunk of data which will eventually be written to disk.  I've tried pointers to structures, first creating the structure and then passing it to the subroutine to be initialized, and other approaches; none seem to work either in actual practice or getting the compiler to accept the code.

Here is what I'd like to do:

struct tag  /* global structure definition */
  {
  int element;
  int counter;
  char somestuff;
  ...
  };

main( args )
{
  struct tag st_list[4];
  ...
st_list call my_subroutine();  //call subroutine to create and initialize multiple structures

  if (st_list[0]->element = 32)
      do something cool;
  if (st_list[4]->counter = 0)
       do something here;
// address all members of all structures...
}
st_list my_subroutine(void)
{
   struct tag my_struct[4]

   my_struct[0].element = 200;
   my_struct[0].counter=99;
   my_struct92].element = 333;
   ...
  //naturally I'd wrap this in a for loop...
  ...more code
    return (*my_struct);  /* return structures to main routine so it can see/modify contents */
}

Thanks in advance.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Passing arrays of structures...
« Reply #1 on: December 12, 2014, 11:01:41 AM »
Something like this?
Code: [Select]
#include <stdio.h>

struct tag  /* global structure definition */
  {
  int element;
  int counter;
  char somestuff;
  };

struct tag* my_subroutine(struct tag st_lst[], int n);

struct tag st_list[4];

int main(void)
{
  //struct tag st_list[4];
  //st_list call
  my_subroutine((struct tag *)&st_list, 4);  //call subroutine to create and initialize multiple structures

  printf("%d %d\n", st_list[0].element, st_list[0].counter);
  if (st_list[0].element == 32)
      ; //do something cool;
  if (st_list[3].counter == 0)
      ; //do something here;
// address all members of all structures...
  return 0;
}

struct tag* my_subroutine(struct tag st_lst[], int n)
{
   st_lst[0].element = 200;
   st_lst[0].counter=99;
   st_lst[3].element = 333;
   //...
   //naturally I'd wrap this in a for loop...
   //...more code
   return st_lst;  /* return structures to main routine so it can see/modify contents */
}
May the source be with you

PhilG57

  • Guest
Re: Passing arrays of structures...
« Reply #2 on: December 14, 2014, 11:06:29 PM »
Nice! Thank you.  I swear I tried every different conbination of structs and pointers to structs trying to get this thing to run.  With your suggestions, it does compile cleanly and does seem to run well.  I still have more testing to do as the code snippets I supplied are just a piece of a larger puzzle, but at least I'm no longer tearing out my hair on this one.

Thanks again.  And especially thanks from one cat lover to another...