473,503 Members | 5,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ - dynamically named structure instances

11 New Member
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
4 2080
Savage
1,764 Recognized Expert Top Contributor
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
Darryl
86 New Member
You can't dynamically create variable names. I believe you have mis-interpreted your assignment.
May 31 '07 #3
Banfa
9,065 Recognized Expert Moderator Expert
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
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

5
5651
by: grondaia | last post by:
Here's my situation: I have an ODBC DSN setup for Timberline Data (An accounting package that uses pervasive.sql) on my sql box. I set up a linked server using the supplied timberline odbc...
8
2928
by: Kevin Little | last post by:
#!/usr/bin/env python ''' I want to dynamically add or replace bound methods in a class. I want the modifications to be immediately effective across all instances, whether created before or...
3
10767
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
6
5436
by: Michael C | last post by:
Hi all, I am currently using a Win32 API call to NetServerEnum to enumerate all SQL Servers on an NT Domain. The function returns the names of all servers running SQL Server on the network, but...
0
1262
by: Michael C | last post by:
Hi all, After a lot of research, I've come to some realizations about enumerating Named Instances of MSDE. NetServerEnum() Win32 API function doesn't work, SQL-DMO doesn't work, no standard...
2
2170
by: Thomas W. Brown | last post by:
If I am using the CSharpCodeProvider to dynamically compile an in-memory assembly from some C# source, do I need to worry about signing this assembly if I'm doing the compilation, instantiation,...
10
4967
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
36
2655
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
6
11057
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.