Solved

Structure Arrays and passing those arrays to a function

Posted on 2006-11-16
4
273 Views
Last Modified: 2010-04-15
I need some help with the proper technique to do the following

1. Build an array of structures of dynamic size (using malloc)
2. Alter that array through user defined functions (passing the array)

Thanks,
 
0
Comment
Question by:bbcac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 125 total points
ID: 17958680
hi

i guess when you say alter the array you mean alter it's values
lets say that your struct name is struct1, and that contains the data
all we need to remember that in C, an array is actually a pointer,
so this is what we do :

struct1** my_array;          // this will define the array
int num_of_items;             // num of items to allocate
int i;

my_array = (struct**) malloc (num_of_items * sizeof(struct1*));        // this will allocate an array of pointers that will  point to the data

for (i=0; i < num_of_items; ++i) {
   my_array[i] = (struct*) malloc (sizeof(struct1));
}

now you have the array of structs
whenever you want to pass the array to a function that will manipulate it, pass as argument the my_array variable and the num_of_items
that is because you can not pass an array of unknow size to a function (The compiler will fail)

momi
0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 125 total points
ID: 17958725

Hi bbac,

Ok.  One step at a time.

1....

//  Define a couple of structures of different sizes

typedef struct
{
  int   Type;
  int   MyInt;
}  struct1;

typedef struct
{
  int      Type;
  char   MyName[100];
}  struct 2;

//  Create a union of the structs

union
{
  struct1  IntStruct;
  struct2  NameStruct;
}  union_t;

//  List of union pointers.

uniont_t **UnionList = NULL;
union_t  *MyUnion;
int    UnionCount = 0;

// Now it's just a matter of putting the items in the list.

//  Add a struct 1

  MyUnion = (union_t *)malloc (sizeof (struct1));
  MyUnion->Type = 0;
  UnionList = (union_t **)realloc (UnionList, sizeof (UnionList*) * (UnionCount + 1);
  UnionList[UnionCount] = MyUnion;
  ++UnionCount;

//  Add a struct 2

  MyUnion = (union_t *)malloc (sizeof (struct2));
  MyUnion->Type = 1;
  UnionList = (union_t **)realloc (UnionList, sizeof (UnionList*) * (UnionCount + 1);
  UnionList[UnionCount] = MyUnion;
  ++UnionCount;


Good Luck,
Kent
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question