Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating structs on the fly 1

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
Hi you will have to forgive me if this is kinda a dumb question but I am more of a C++ programmer than a C programmer...

What I want to do is have a struct:

struct state {
int seed;
int who;
int lock;
};

and create an array of then according to user input so something like this:

int main()
{
int number;
printf ("How many?");
scanf ("%i", );
struct state Readers[number];
}


Is there a way to do this?

Thanks!
Cory

 
Cory,

My C is a little rusty but you may not be able to use a variable in an array dimension declaration. If you can't you want to use a pointer and allocate the memory like this.

int number = ....; /*now contains a valid number*/
struct state * Readers = (struct state*)malloc( sizeof(struct state) * number);

memset( Readers, 0, sizeof(struct state) * number);

Hope this helps
-pete
 
Yes, you need to use a constant expression to initialize a statically allocated array. I believe that C99 (the new ANSI standard) allows you to create variable length arrays, but not C89 and since there aren't any C99 compilers yet...

So, if you want the user to specify the number, you'll have to do as palbano suggested and dynamically allocate your array.

Another option is to use a linked list (or similiar structure) to store each struct, then you don't have to know how many structs you will eventually need at all. This is preferable as it's generally not good to bother the user with these details. It's a bit more work to implement it this way, but worth it.

A "middle of the road" solution is not to ask the user the number and use malloc() and realloc() as needed by initializing the array of structs with a predetermined block size and then "growing" it as needed:

/* Untested and some stuff omitted for brevity */

#include <stddef.h>
#include <stdlib.h>

#define CHUNK_SIZE 20 /* or whatever's appropriate */

struct state {
int seed;
int who;
int lock;
};

/* ... */

struct state *foo=malloc(sizeof *foo * CHUNK_SIZE);
size_t blockSize=CHUNK_SIZE; /* Keeps track of how much
* memory we've allocated
*/
size_t fooSize=0; /* Keeps track of how many
* members are in the struct
*/

/* Enter data retrival loop */
for (;;) {
/* Get user input, breaking from the loop if
* there isn't any more
*/
if (fooSize+1 > blockSize) {
/* To add another element, we need to increase
* the memory for foo
*/
struct state *tmp=realloc(foo,blockSize+CHUNK_SIZE);
if (NULL!=tmp) {
foo=tmp;
blockSize+=CHUNK_SIZE;
} else {
/* handle error and exit loop */
}
}
/* Assign input to current element of foo,
* incrementing fooSize by 1 for each
* assignment
*/
}

The big advantage that a linked list has over an array here is if you need to delete elements from your array, the operation is slow, particularly if there are a lot of elements.

HTH,



Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top