Welcome! Log In Create A New Profile

Advanced

Array of Structures with Unique Structures?

Posted by LordAshes 
Array of Structures with Unique Structures?
March 07, 2010 05:10AM
I am writting my code in C. I am an intermediate C programmer so I am not sure if this is an easy task where I have overlooked something obvious or if this is more difficult that is sounds.

I am modifying previously written code so I am somewhat limited in what I can do. I will use a simplified example to illustrate what I am trying to accomplish.

Say I have one structure (Client) with infromation on clients:

First Name
Last Name
Account Number

The program defines an array of this structure so that many clients can be recorded.

Now I want to add additional information to this structure but this infromation will vary. Let say it can have one of three possible structures:

Additional structure type 1 (Contact_Phone):

Country
Area
Phone

Additional structure type 2 (Contact_Mail):

Street
City
Provine
Country

Additional structure type 3 (Contact_Email):

Address


So Ideally I want to keep the array of Clients (the first structure) otherwise I would have to change a tonne of code. However, I would like to add to this a generic pointer that will point to one of the 3 additional structures. However, since the type can be different for each member of the Client array, this pointer needs to be generic in the initial definition. Then when the client data is actually populated, an instance of one of the addition structures would be created, populated and then that particular client's pointer would be set to point to it.

So I have something like:

typedef struct
{
string First;
string Last;
int Account;
int *Contacts;
} client_type;

typedef struct
{
int Country;
int Area;
int Phone;
} contact_1;

typedef struct
{
string Street;
string City;
string Province;
string Country;
u32
} contact_2;

typedef struct
{
string Address;
} contact_3;

client_type clients[200]; // Define array for 200 clients

clients[0].First="Joe"; // Populate first client
clients[0].Last="Brown";
clients[0].Account=12345;

contact_3 *temp = memalloc(sizeof(contact_3)); // Allocate memory for the contact info
*temp->Address="JoeBrown@Hotmail.Com"; // Populate the contact info

So now how do I set the Contact pointer to point to my contact structure. If I do something like:

clients[0].Contact = *temp;

then the address link will be there but the original definition did not specify the structure so how will clients[0] know that Contact now points to a contact_3 type structure so that I can use something like clients[0].Contact->Address? Do I need to type cast in the assignment or something?
Re: Array of Structures with Unique Structures?
March 07, 2010 06:57AM

typedef struct
{
string First;
string Last;
int Account;
int *Contacts;
} client_type;

If Contacts is pointing to another struct the data type should be the struct not a pointer to an int. If you don't know what it is you can do a void* and cast it back later.

You can try this, I'm not sure it true C code as I'm more C++ myself,


typedef struct
{
  int Country;
  int Area;
  int Phone;
} contact_1;

typedef struct
{
  string Street;
  string City;
  string Province;
  string Country;
} contact_2;

typedef struct
{
  string Address;
} contact_3;

typedef struct
{
  string First;
  string Last;
  int Account;

  contact_1* pcontact1;
  contact_2* pcontact3;
  contact_3* pcontact3;

} client_type;


client_type clients[200]; // Define array for 200 clients

contact_1 *temp1 = malloc(sizeof(contact_1));
contact_2 *temp2 = malloc(sizeof(contact_2));
contact_3 *temp2 = malloc(sizeof(contact_3));

clients[0].First="Joe"; // Populate first client
clients[0].Last="Brown";
clients[0].Account=12345;

// access to all
clients[0]->pcontact1 = temp1;
clients[0]->pcontact2 = temp2;
clients[0]->pcontact3 = temp3;

clients[0]->pcontact1->Address="JoeBrown@Hotmail.Com"; // Populate the contact info

.....
.....

free(clients[0]->pcontact1);
free(clients[0]->pcontact2);
free(clients[0]->pcontact3);


P.S. This is more offtopic as it does not specifically relate to Wii development.



Edited 1 time(s). Last edit at 03/07/2010 06:58AM by scanff.
Re: Array of Structures with Unique Structures?
March 07, 2010 10:33AM
Sorry. I guess it is. The code I am modifying is for the Wii but you are correct that the question itself is not Wii related.

Thanks for the advice. The original structure (clients) will each have only one of the contact structures and each one can have a different one, so I think I need to use your advice of making it void and then setting it later when I populate it.

Once again sorry for my off topic post.

(Note To Admin: Please feel free to remove this post or move it to a different section if it is not appropriate)



Edited 1 time(s). Last edit at 03/07/2010 10:33AM by LordAshes.
Re: Array of Structures with Unique Structures?
March 08, 2010 08:38AM
Sounds like a perfect place to use a union. But how do you keep a record of which contact type is used for each client? If you cast it to void* you have to know what to cast back to when you want to read it...
Re: Array of Structures with Unique Structures?
March 08, 2010 11:02AM
Yes. I am only using client as an example.

The code is actually for an unofficial upgrade to WGS (a gui based program that helps beginners write programs for the Wii). I would like to implement object level variables for this program. The object structure is already set by WGS but I can add to it. So to answer your question, the user of the program will remember which objects have which additional variables (in the example contact types) because he/she will be the one defining them.
Sorry, only registered users may post in this forum.

Click here to login