M
Marc Boyer
Le 13-02-2007 said:Is something like this possible? Is there another way?
An array ?
Marc Boyer
Le 13-02-2007 said:Is something like this possible? Is there another way?
Nate said: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....
}
Is something like this possible? Is there another way?
Christopher said:No, theres no "good" way. Your way, in particular, is wrong. C
doesn't work like that.
Yes; there's a bad (IMHO) way:
#define DECLARE1(type,var) type var##1
#define DECLARE2(type,var) DECLARE1(type,var); \
type var##2
#define DECLARE3(type,var) DECLARE2(type,var); \
type var##3
int main( void ) {
DECLARE3(int,foo);
return 0;
}
Joe Estock said:Or the [slightly] more maintainable way:
#define DECLARE(var, seq) var##seq
int main(void)
{
int DECLARE(foo, 1);
int DECLARE(foo, 2);
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,
Nate said: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("variables.c","w");
for (int i=0; i<100; i++) {
fprintf(f,"int variable%d\n",i);
}
fclose(f);
}
Mark said:There isn't - you can't define object names at runtime in C.
Yes, see my answer to McIntyreKenny said: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.
Christopher said:Joe Estock said:Or the [slightly] more maintainable way:#define DECLARE(var, seq) var##seqint 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?
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,"struct dynamicobject { char *name;int len; };\n");
// 2 Define an object of that type
fprintf(f,"struct 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(dllexport) \n");
fprintf("GetDynamicObject(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("variables.dll");
// Get the address of the created function in the shared object
fn = (void (*fn)(int))GetProcAddress("GetDynamicObject");
// 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.
Personally I would use an array of type int;
I
was distracted by the vast amount of snow outside when I replied to your
original thread.
#include <stdio.h>
int main(void)
{
FILE *f = fopen("variables.c","w");
for (int i=0; i<100; i++) {
fprintf(f,"int variable%d\n",i);
}
fclose(f);
}
jacob navia wrote On 02/13/07 18:05,:
Nate wrote:
#include <stdio.h>
int main(void)
{
FILE *f = fopen("variables.c","w");
for (int i=0; i<100; i++) {
fprintf(f,"int variable%d\n",i);
}
fclose(f);
}
"variables.c", line 2: undefined or not a type: variable0
"variables.c", line 3: parameter not in identifier list: variable1
"variables.c", line 3: syntax error before or at: int
"variables.c", line 4: parameter not in identifier list: variable2
[...]
"variables.c", line 99: syntax error before or at: int
"variables.c", line 100: parameter not in identifier list: variable98
"variables.c", line 101: parameter not in identifier list: variable99
"variables.c", line 101: syntax error before or at: <EOF>
cc: acomp failed for variables.c
One hundred fifty-one diagnostics all told. An impressive
yield for an investment of a mere nine lines of code.
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?
For example, this loop would declare the following variables all of
type int:
Is something like this possible? Is there another way?
int variable[4];
Then access them like this:
variable[0] = 4;
variable[1] = 3;
...
variable[3] = 10;
The numbers will be 0 through to one less than the parameter
you specified in the declaration.
If you don't know how many you will need until runtime
then you can write:
int *variable = malloc( 4 * sizeof *variable );
and then access them in the same way as before.
Nate said:For example, this loop would declare the following variables all of
type int:
Is something like this possible? Is there another way?
int variable[4];
Then access them like this:
variable[0] = 4;
variable[1] = 3;
...
variable[3] = 10;
<snip>OW,
This solution will work perfectly for what I'm trying to do, thank you
for your suggestion.
Wrong
#include <stdio.h>
#include <windows.h>
void *h = LoadLibrary("variables.dll");
fn = (void (*fn)(int))GetProcAddress("GetDynamicObject");
Mark said:Right.
Agreed.
Beep beep. This isn't C.
This isn't either.
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.