Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Automatically generate variables

Reply
Thread Tools

Automatically generate variables

 
 
Marc Boyer
Guest
Posts: n/a
 
      02-13-2007
Le 13-02-2007, Nate <(E-Mail Removed)> a écrit*:
> Is something like this possible? Is there another way?


An array ?

Marc Boyer
 
Reply With Quote
 
 
 
 
Nate
Guest
Posts: n/a
 
      02-13-2007
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

 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-13-2007
Nate <(E-Mail Removed)> wrote:

> 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,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;
}

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)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Joe Estock
Guest
Posts: n/a
 
      02-13-2007
Christopher Benson-Manica wrote:
> Nate <(E-Mail Removed)> wrote:
>
>> 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,var) type var##1
> #define DECLARE2(type,var) DECLARE1(type,var); \
> type var##2
> #define DECLARE3(type,var) DECLARE2(type,var); \
> type var##3


Or the [slightly] more maintainable way:

#define DECLARE(var, seq) var##seq

>
> int main( void ) {
> DECLARE3(int,foo);
> 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.
>

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-13-2007
Joe Estock <(E-Mail Removed)> wrote:

> 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)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      02-13-2007
On 13 Feb 2007 08:35:59 -0800, in comp.lang.c , "Nate"
<(E-Mail Removed)> 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,


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
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      02-13-2007
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("variables.c","w");
for (int i=0; i<100; i++) {
fprintf(f,"int variable%d\n",i);
}
fclose(f);
}
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      02-13-2007
In article <45d2443a$0$27375$(E-Mail Removed)>,
jacob navia <(E-Mail Removed)> wrote:
>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("variables.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.

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      02-13-2007
Mark McIntyre wrote:
> On 13 Feb 2007 08:35:59 -0800, in comp.lang.c , "Nate"
> <(E-Mail Removed)> 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,

>
>
> 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,"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.


 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      02-13-2007
Kenny McCormack wrote:
> In article <45d2443a$0$27375$(E-Mail Removed)>,
> jacob navia <(E-Mail Removed)> wrote:
>
>>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("variables.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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatically generate BLL for ObjectDataSource? tomerdr@gmail.com ASP .Net 0 04-28-2007 01:59 PM
Automatically generate file names Freddy C Programming 6 03-27-2007 08:05 AM
DataGrid with id could not automatically generate any columns from the selected data source. Beffmans ASP .Net 1 09-23-2005 08:09 AM
Re: automatically generate a website out of a database using parameters lkrubner@geocities.com XML 0 12-08-2004 05:08 AM
Re: automatically generate a website out of a database using parameters rumionfire@gmail.com XML 0 12-06-2004 04:21 PM



Advertisments