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.