473,651 Members | 2,630 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 2089
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
5659
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 driver. I have two sql instances setup, the default instance and a named instance. On the default instance, the linked server works great no matter who is logged in using it (all authentication is NT integrated). However on the named instance only...
8
2939
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 after the class was modified. I need this to work for both old ('classic') and new style classes, at both 2.3 and 2.4. I of course want to avoid side effects, and to make the solution as light-weight as possible.
3
10795
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 then like that, when clicking the LinkButton, the user can be navigated to another page, carrying a variable. I would like to use server.transfer method instead of QueryString as I don't want the carried variable to be visible for the user.
6
5441
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 it doesn't return any SQL Server/MSDE named instances. Does anyone have any ideas on how to enumerate all named instances of SQL Server/MSDE on a Domain as well as the default instances? Thanks, Michael C.
0
1271
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 routines seem to work. SQL-DMO is by far the most robust solution - since it calls NetServerEnum() first, and subsequently sends out a broadcast on Port 1433 to locate all SQL Servers on the network. This does pick up all default instances (as long...
2
2176
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, and invocation of a dynamic object from a strongly named assembly? If so, how do you strongly name a dynamic, in-memory assembly?
10
4983
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 get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
36
2698
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 on it's own. However when I call the child object I get an error " has no properties (in firefox)." I simple test:
6
11068
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 how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
0
8795
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8576
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.