By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

C++ - dynamically named structure instances

P: 11
I have a structure, say
typedef struct
{
char a[10];
char b[10];
int i;
} test;

Based on the user input of how many instances to create , i should create instances prefixed with runtime data.
For ex., if user says 3 which has to be prefixed with "US", i need to create

test *UStesting1;
test *UStesting2;
test *UStesting3;

and i need to load various file into each instance.
For e.g, USTesting1 will contain data from File US1.in;
USTesting2 will contain data from File US2.in;
& USTesting2 will contain data from File US3.in;

Again, i'm using malloc and free to decide the size of the array structure based on the number of entries in input file.

How can i do this?

Can anyone help me on this, since this is a bit urgent!!

Thanks in advance,
Priya
May 31 '07 #1
Share this Question
Share on Google+
4 Replies


Savage
Expert 100+
P: 1,764
I have a structure, say
typedef struct
{
char a[10];
char b[10];
int i;
} test;

Based on the user input of how many instances to create , i should create instances prefixed with runtime data.
For ex., if user says 3 which has to be prefixed with "US", i need to create

test *UStesting1;
test *UStesting2;
test *UStesting3;

and i need to load various file into each instance.
For e.g, USTesting1 will contain data from File US1.in;
USTesting2 will contain data from File US2.in;
& USTesting2 will contain data from File US3.in;

Again, i'm using malloc and free to decide the size of the array structure based on the number of entries in input file.

How can i do this?

Can anyone help me on this, since this is a bit urgent!!

Thanks in advance,
Priya
U will need to run a programm that will edit code of programm which have class instances then compile it in command line and run it.

Now,which compiler do u use?

Savage
May 31 '07 #2

P: 86
You can't dynamically create variable names. I believe you have mis-interpreted your assignment.
May 31 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
I have a structure, say
typedef struct
{
char a[10];
char b[10];
int i;
} test;

Based on the user input of how many instances to create , i should create instances prefixed with runtime data.
For ex., if user says 3 which has to be prefixed with "US", i need to create

test *UStesting1;
test *UStesting2;
test *UStesting3;

and i need to load various file into each instance.
For e.g, USTesting1 will contain data from File US1.in;
USTesting2 will contain data from File US2.in;
& USTesting2 will contain data from File US3.in;

Again, i'm using malloc and free to decide the size of the array structure based on the number of entries in input file.

How can i do this?
I would create a containing structure

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.     char prefix[10];
  4.     int count;
  5.     test *array;
  6. } test_container;
  7.  
Then you can fill in prefix and count with input from the user. Use count to malloc an array of test structures and store in the array member. Then you can iterate through each of these structures open the file and fill in the structure.

Pseudo coded as

Expand|Select|Wrap|Line Numbers
  1. test_container container;
  2.  
  3. container.prefix = GetPrefixFromUser();
  4.  
  5. container.count = GetCoutFromUser();
  6.  
  7. container.array = (test *)malloc(sizeof *container.array * continer.count);
  8.  
  9. for ix = 0 to count -1
  10. {
  11.     filename = continer.prefix + (ix+1) + ".in"
  12.  
  13.     Load continer.array[ix] from filename
  14. }
  15.  
May 31 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Going back to the original problem, if the user says 3 you need to have:

Expand|Select|Wrap|Line Numbers
  1. test *UStesting1;
  2. test *UStesting2;
  3. test *UStesting3;
  4.  
So, you malloc() and array of 3 test:

Expand|Select|Wrap|Line Numbers
  1. test* arr = (test*)malloc(3 * sizeof(test));
  2.  
Yes?

So you use arr[0] instead of UStesting1, arr[1] instead of UStesting2 and arr[2 ] instead of UStesting3.

You cannot create variable names at run time but you can allocate an array whose size won't be known until run time and if you do that, the array elements can be used as variable names.
May 31 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.