473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Structure Creation

RSR
I want to be able to create an unknown amount of variables depending
on the users input. For example, if the user enters 10 from the
command line, I want to be able to create 10 structures with different
names. In other words, is there a way to append a number on the the
end of the structure name to make it unique. Ex) struct1,
struct2,...,sturct10.
Nov 13 '05 #1
7 2901
On 11 Oct 2003 16:06:18 -0700, ra******@hotmail.com (RSR) wrote:
I want to be able to create an unknown amount of variables depending
on the users input. For example, if the user enters 10 from the
command line, I want to be able to create 10 structures with different
names. In other words, is there a way to append a number on the the
end of the structure name to make it unique. Ex) struct1,
struct2,...,sturct10.

Names exist only during the compile phase, not during execution. You
can achieve the same affect with

/* remember to include stdlib.h */
struct s {...};
struct s *ptr;
int num, i;
/* obtain desired number of structures and store in num */
ptr = malloc(num * sizeof *ptr);
if (ptr == NULL) {/* error handler */}
/* at this point, you can let i vary from 0 to num-1 and ptr[i]
will be a different structure each time*/
<<Remove the del for email>>
Nov 13 '05 #2
RSR wrote:
I want to be able to create an unknown amount of variables depending
on the users input. For example, if the user enters 10 from the
command line, I want to be able to create 10 structures with different
names. In other words, is there a way to append a number on the the
end of the structure name to make it unique. Ex) struct1,
struct2,...,sturct10.


Not exactly. However, you could dynamically allocate an array. For
example:

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

int main(void)
{
typedef struct { unsigned int x; } Foo;
unsigned int numberOfFoos = 0;
Foo *foos = NULL;
printf("Please enter the number of Foos: ");
fflush(stdout);
scanf("%u", &numberOfFoos); /* Should check return value. */
foos = malloc(numberOfFoos * sizeof(*foos));
if (foos != NULL)
{
unsigned int i;
for (i = 0; i < numberOfFoos; ++i)
{
foos[i].x = i + 1;
printf("foo[%u].x = %u\n", i, foos[i].x);
}
free(foos);
}
else
{
fprintf(stderr, "Could not allocate memory.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Nov 13 '05 #3
On 11 Oct 2003 16:06:18 -0700, in comp.lang.c , ra******@hotmail.com
(RSR) wrote:
I want to be able to create an unknown amount of variables depending
on the users input.
Do some reading on linked lists.
For example, if the user enters 10 from the
command line, I want to be able to create 10 structures with different
names.
Not possible*
In other words, is there a way to append a number on the the
end of the structure name to make it unique. Ex) struct1,
struct2,...,sturct10.


Names are removed during compilation, so there's no way to do this.
* well, yo could write code, that when executed wrote a new C source
file with the relevant variables in it, compiled that new file, and
executed the output. But that would be truly horrible.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #4
"Mark McIntyre" <ma**********@spamcop.net> wrote:
* well, yo could write code, that when executed wrote a new C source
file with the relevant variables in it, compiled that new file, and
executed the output. But that would be truly horrible.


I once implemented an arbitrary expression parser that way in QBasic.

Fun fun fun...

--
Simon.
Nov 13 '05 #5
"Russell Hanneken" <rg********@pobox.com> wrote in message news:<Gn*****************@newsread4.news.pas.earth link.net>...
RSR wrote:
I want to be able to create an unknown amount of variables depending
on the users input. For example, if the user enters 10 from the
command line, I want to be able to create 10 structures with different
names. In other words, is there a way to append a number on the the
end of the structure name to make it unique. Ex) struct1,
struct2,...,sturct10.

Follow the example provided by
Mr.Russell Hanneken with cmd line args parsing.
Not exactly. However, you could dynamically allocate an array. For
Yes you can but only when the array's elements are pointer which
points to a value.
In your example you decleard *foos and malloc()ed. After that you
are assigning array's elements dynimically. There are lot of
difference detween dynamacly allocating memmory and variouble sized
array.
Follow the thread with the titles
1)Can array[]=malloc()ed?
2)Variable length array confusion

in comp.lang.c
Thanks
example:

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

int main(void)
{
typedef struct { unsigned int x; } Foo;
unsigned int numberOfFoos = 0;
Foo *foos = NULL;
printf("Please enter the number of Foos: ");
fflush(stdout);
scanf("%u", &numberOfFoos); /* Should check return value. */
foos = malloc(numberOfFoos * sizeof(*foos));
if (foos != NULL)
{
unsigned int i;
for (i = 0; i < numberOfFoos; ++i)
{
foos[i].x = i + 1;
printf("foo[%u].x = %u\n", i, foos[i].x);
}
free(foos);
}
else
{
fprintf(stderr, "Could not allocate memory.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

Nov 13 '05 #6
"Russell Hanneken" <rg********@pobox.com> wrote in message news:<Gn*****************@newsread4.news.pas.earth link.net>...
RSR wrote:
I want to be able to create an unknown amount of variables depending
on the users input. For example, if the user enters 10 from the
command line, I want to be able to create 10 structures with different
names. In other words, is there a way to append a number on the the
end of the structure name to make it unique. Ex) struct1,
struct2,...,sturct10.

Follow the example provided by
Mr.Russell Hanneken with cmd line args parsing.
Not exactly. However, you could dynamically allocate an array. For
Yes you can but only when the array's elements are pointer which
points to a value.
In your example you decleard *foos and malloc()ed. After that you
are assigning array's elements dynimically. There are lot of
difference detween dynamacly allocating memmory and variouble sized
array.
Follow the thread with the titles
1)Can array[]=malloc()ed?
2)Variable length array confusion

in comp.lang.c
Thanks
example:

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

int main(void)
{
typedef struct { unsigned int x; } Foo;
unsigned int numberOfFoos = 0;
Foo *foos = NULL;
printf("Please enter the number of Foos: ");
fflush(stdout);
scanf("%u", &numberOfFoos); /* Should check return value. */
foos = malloc(numberOfFoos * sizeof(*foos));
if (foos != NULL)
{
unsigned int i;
for (i = 0; i < numberOfFoos; ++i)
{
foos[i].x = i + 1;
printf("foo[%u].x = %u\n", i, foos[i].x);
}
free(foos);
}
else
{
fprintf(stderr, "Could not allocate memory.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

Nov 13 '05 #7
RSR wrote:
I want to be able to create an unknown amount of variables depending
on the users input. For example, if the user enters 10 from the
command line, I want to be able to create 10 structures with different
names. In other words, is there a way to append a number on the the
end of the structure name to make it unique. Ex) struct1,
struct2,...,sturct10.


You can't do that.

However, C has this terribly useful notion of an *array* of like
objects accessed by an integer index, which seems as if it would
solve your problem as easily as a hot knife slices through beer.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dariusz | last post by:
I have written a database that counts the number of times a file has been accessed, so I can then later display the results on what is "hot" and what is not. At the moment all it does is count the...
5
by: megha | last post by:
Hi I want to create a tree type structure for my folders directory in java how i can do it. I know it will be a big programming but how to start please advice.
2
by: spamproof2005 | last post by:
Is there a way to copy the structure from one database to another without affecting the actual data? For example, I added new fields and stored procedures to a db in my development environment....
13
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char...
0
by: Jon Rea | last post by:
I have an MDI application that can have many child windows, although each "document" does not nececarily just have one window. There are a few issues that I have. I want multi-threading...
9
by: Dieter | last post by:
Hi All, I am trying to dynamically implement (declare/define) a structure within a function. The reason being, is that this structure would contain a variable number of members of various...
0
by: m3rajk | last post by:
I've recently become the defacto DBA of two MSSQL databases at work because I am the only one with SQL experience. I have been asked to do some tasks with the databases but this requires and...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
5
by: hnshashi | last post by:
I have writtem kernel(2.4) module to commu. with user space appl. using netlink socket. I am getting compilation error. kernel module:-> #include <linux/skbuff.h> #include<linux/module.h> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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
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...

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.