473,774 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automatically generate variables

Hello,

I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this, but I had something
like this in mind...

int i;

for(i = 1; i < 4; i++){

int variable....

}

For example, this loop would declare the following variables all of
type int:

variable1
variable2
variable3

Is something like this possible? Is there another way?

Thanks in advance,

-Nate

Feb 13 '07 #1
111 4678
Le 13-02-2007, Nate <nv******@sbcgl obal.neta écrit*:
Is something like this possible? Is there another way?
An array ?

Marc Boyer
Feb 13 '07 #2
Nate <nv******@sbcgl obal.netwrote:
I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this, but I had something
like this in mind...
No, theres no "good" way. Your way, in particular, is wrong. C
doesn't work like that.
int i;
for(i = 1; i < 4; i++){
int variable....
}
Is something like this possible? Is there another way?
Yes; there's a bad (IMHO) way:

#define DECLARE1(type,v ar) type var##1
#define DECLARE2(type,v ar) DECLARE1(type,v ar); \
type var##2
#define DECLARE3(type,v ar) DECLARE2(type,v ar); \
type var##3

int main( void ) {
DECLARE3(int,fo o);
return 0;
}

You could generate as many #defines as you please using some other
program (say a shell script) and put them in a separate header file.

Alternatively, you could tell us what you *really* want to do and we
can probably suggest a much better alternative.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Feb 13 '07 #3
Christopher Benson-Manica wrote:
Nate <nv******@sbcgl obal.netwrote:
>I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this, but I had something
like this in mind...

No, theres no "good" way. Your way, in particular, is wrong. C
doesn't work like that.
>int i;
>for(i = 1; i < 4; i++){
int variable....
}
>Is something like this possible? Is there another way?

Yes; there's a bad (IMHO) way:

#define DECLARE1(type,v ar) type var##1
#define DECLARE2(type,v ar) DECLARE1(type,v ar); \
type var##2
#define DECLARE3(type,v ar) DECLARE2(type,v ar); \
type var##3
Or the [slightly] more maintainable way:

#define DECLARE(var, seq) var##seq
>
int main( void ) {
DECLARE3(int,fo o);
return 0;
}
int main(void)
{
int DECLARE(foo, 1);
int DECLARE(foo, 2);

DECLARE(foo, 1) = 10;
foo2 = 5;

printf("foo1: %d foo2: %d\n", DECLARE(foo, 1), foo2);

return(0);
}
>
You could generate as many #defines as you please using some other
program (say a shell script) and put them in a separate header file.

Alternatively, you could tell us what you *really* want to do and we
can probably suggest a much better alternative.
Feb 13 '07 #4
Joe Estock <je*****@nospam nutextonline.co mwrote:
Or the [slightly] more maintainable way:
#define DECLARE(var, seq) var##seq
int main(void)
{
int DECLARE(foo, 1);
int DECLARE(foo, 2);
It's more maintainable, yes, but it also rather defeats the purpose of
the whole exercise, doesn't it? If one wants int foo1 through int
foo100, what's the added benefit of invoking the macro 100 times as
opposed to simply writing the actual declarations?

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Feb 13 '07 #5
On 13 Feb 2007 08:35:59 -0800, in comp.lang.c , "Nate"
<nv******@sbcgl obal.netwrote:
>Hello,

I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this,
There isn't - you can't define object names at runtime in C.

You could mallocate an array of objects however, and size it to
whatever size you needed. This is likely to be more useful anyway as
the chances are you will need to loop over the variables.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 13 '07 #6
Nate wrote:
Hello,

I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this, but I had something
like this in mind...

int i;

for(i = 1; i < 4; i++){

int variable....

}

For example, this loop would declare the following variables all of
type int:

variable1
variable2
variable3

Is something like this possible? Is there another way?

Thanks in advance,

-Nate
#include <stdio.h>
int main(void)
{
FILE *f = fopen("variable s.c","w");
for (int i=0; i<100; i++) {
fprintf(f,"int variable%d\n",i );
}
fclose(f);
}
Feb 13 '07 #7
In article <45************ ***********@new s.orange.fr>,
jacob navia <ja***@jacob.re mcomp.frwrote:
>Nate wrote:
>Hello,

I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this, but I had something
like this in mind...

int i;

for(i = 1; i < 4; i++){

int variable....

}

For example, this loop would declare the following variables all of
type int:

variable1
variable2
variable3

Is something like this possible? Is there another way?

Thanks in advance,

-Nate

#include <stdio.h>
int main(void)
{
FILE *f = fopen("variable s.c","w");
for (int i=0; i<100; i++) {
fprintf(f,"int variable%d\n",i );
}
fclose(f);
}
You need to finish what you started. IOW, you forgot to mention:
1) And then compile variables.c as a shared library
2) And then dlopen that library
3) And then enjoy your new symbols

All OT, blah, blah, blah, of course.

Feb 13 '07 #8
Mark McIntyre wrote:
On 13 Feb 2007 08:35:59 -0800, in comp.lang.c , "Nate"
<nv******@sbcgl obal.netwrote:

>>Hello,

I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this,


There isn't - you can't define object names at runtime in C.

Wrong

#include <stdio.h>
#include <windows.h>
int main(void)
{
FILE *f = fopen("dynamic. c","w");
void (*fn)(void);
void *ptr;

// 1 Define some structure for instance
fprintf(f,"stru ct dynamicobject { char *name;int len; };\n");
// 2 Define an object of that type
fprintf(f,"stru ct dynamicobject var = {\"NoName\",6}; \n");
// 3 Define an exported function in a shared object that returns the
// address of the created object
fprintf(f,"void * __declspec(dlle xport) \n");
fprintf("GetDyn amicObject(void )\n\treturn &var;}\n");
// Done Close the file
fclose(f);
// Compile it. The compiler can change of course :-)
system("lcc dynamic.c");
// Link it into a shared object
system("lcclnk -dll dynamic.obj");
// Open the shared object (dlopen under Unix)
void *h = LoadLibrary("va riables.dll");
// Get the address of the created function in the shared object
fn = (void (*fn)(int))GetP rocAddress("Get DynamicObject") ;
// Call the function we just compile
ptr = fn();
// And now, ladies and gentlemen
// Here I have a pointer to a dynamically created object
}

Most of my customers buy lcc-win32 as a "Just in time compiler", that
allows them to do this much more efficiently than what is shown here

A JIT compiler is specialized in generating code dynamically. For
instance, if you are modelling molecule interaction you can develop
a special language that is task oriented, compile it to C, then
just JIT compile it into a shared object that you run on the fly.

Of course what you generate is an object, not NAMES...
In this sense the OP is completely wrong of course.
Feb 13 '07 #9
Kenny McCormack wrote:
In article <45************ ***********@new s.orange.fr>,
jacob navia <ja***@jacob.re mcomp.frwrote:
>>Nate wrote:
>>>Hello,

I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this, but I had something
like this in mind...

int i;

for(i = 1; i < 4; i++){

int variable....

}

For example, this loop would declare the following variables all of
type int:

variable1
variable2
variable3

Is something like this possible? Is there another way?

Thanks in advance,

-Nate

#include <stdio.h>
int main(void)
{
FILE *f = fopen("variable s.c","w");
for (int i=0; i<100; i++) {
fprintf(f,"int variable%d\n",i );
}
fclose(f);
}


You need to finish what you started. IOW, you forgot to mention:
1) And then compile variables.c as a shared library
2) And then dlopen that library
3) And then enjoy your new symbols

All OT, blah, blah, blah, of course.
Yes, see my answer to McIntyre
Feb 13 '07 #10

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

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.