Structure Creation

R

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.
 
B

Barry Schwarz

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
will be a different structure each time*/


<<Remove the del for email>>
 
R

Russell Hanneken

RSR said:
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.x = i + 1;
printf("foo[%u].x = %u\n", i, foos.x);
}
free(foos);
}
else
{
fprintf(stderr, "Could not allocate memory.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
 
M

Mark McIntyre

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.
 
S

Simon Biber

Mark McIntyre said:
* 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...
 
D

dam_fool_2003

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.x = i + 1;
printf("foo[%u].x = %u\n", i, foos.x);
}
free(foos);
}
else
{
fprintf(stderr, "Could not allocate memory.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
 
D

dam_fool_2003

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.x = i + 1;
printf("foo[%u].x = %u\n", i, foos.x);
}
free(foos);
}
else
{
fprintf(stderr, "Could not allocate memory.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
 
C

Chris Dollin

RSR said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top