Grrr... preprocessor and concatenation blues 
Author Message
 Grrr... preprocessor and concatenation blues

Greetings,

I looked in the FAQ and searched on deja.  I found a million things that were
close to what Im trying to do, but cant find the exact thing.  Any help on this
will be appreciated!

I am trying to use c's macros to do concatenation.  I have some code like this:

#define THORRIN(x) &thorrin ## x

char * thorrin1;
char * thorrin2;
char * thorrin3;
..

for (i=0; i < MAX_THORRIN; i++) {
   printf("Address for thorrin %d is %x\n",i, THORRIN(i));

Quote:
}

This works great if I put in THORRIN(1)... But if I put in THORRIN(i), I get an
error message like...'thorrini not defined'

How can I substitute the variable in the token name?  I've tried a bunch of
combinations including 'stringing' #defines together, but I cant get that
variable to substitue for a real value.  Any ideas?

Thanks!

-Thorrin

"You better practice if you wanna beat me!"
     -Ryudo, Grandia II
--



Wed, 19 Jan 2005 11:45:41 GMT  
 Grrr... preprocessor and concatenation blues

Quote:

> #define THORRIN(x) &thorrin ## x

> char * thorrin1;
> char * thorrin2;
> char * thorrin3;
> ..

> for (i=0; i < MAX_THORRIN; i++) {
>    printf("Address for thorrin %d is %x\n",i, THORRIN(i));
> }

Sorry, it's just not possible.  Macro's are expanded at compile time not
run time, so as you've found out THORRIN(i) becomes &thorrini.

Quote:
>From what it looks like you're trying to do, perhaps something like this

would be better:

char *thorrin[MAX_THORRIN];
...

for (i = 0; i < MAX_THORRIN; ++i) {
  printf("Address for thorrin %d is %x\n", i, &thorrin[i]);

Quote:
}

Unless there is some reason for them to be separate.  Also, are you sure
you want to look at the address of the pointer rather than the address
of the string? (I'm assuming they're strings anyway)  To get the address
of the string instead simply take off the & operator.

HTH
Neal

--
char*a(char*b,char c){while(*b!=c&&*b)++b;return b;}void\
 c(char*b){char*d="HfCENioa.erckml\nbs ";while(*b){*b=*(\
a(d,' ')-a(d,*b)+d);++b;}}int main(){char e[]="lecNH\nrH\
bkkiCefE caoe.Hsk.H m.eE";c(e);printf("%s",e);return 0;}
--



Fri, 21 Jan 2005 20:54:22 GMT  
 Grrr... preprocessor and concatenation blues

Quote:

> I am trying to use c's macros to do concatenation.  I have some code like this:

> #define THORRIN(x) &thorrin ## x

> char * thorrin1;
> char * thorrin2;
> char * thorrin3;
> ..

> for (i=0; i < MAX_THORRIN; i++) {
>    printf("Address for thorrin %d is %x\n",i, THORRIN(i));
> }

> This works great if I put in THORRIN(1)... But if I put in THORRIN(i), I get an
> error message like...'thorrini not defined'

> How can I substitute the variable in the token name?  I've tried a bunch of
> combinations including 'stringing' #defines together, but I cant get that
> variable to substitue for a real value.  Any ideas?

This won't work.  The problem here is that you're trying to mix the
compiler and the preprocessor togeths, when the two actually run one
after the other, only passing processed text between them.

The easiest way to explain the problem here is to imagine that you're
the preprocessor and to step through the substitutions it makes.

Before the compiler processes your program the preprocessor runs.  On
modern compilers the preprocessor tends to be integrated into the
compiler front end, but historically the preprocessor has often been in
a separate executable, separate from the compiler.  This means that when
the preprocessor runs it knows nothing about the actual structure of
your program because it hasn't been parsed yet.

First you define the macro THORRIN.  So the preprocessor stores this
away.  To it THORRIN is a macro that takes a single parameter which is
concatenated onto &thorrin.  Its important to know here that the
preprocessor treats x as text, because all the preprocessor handles is
text.

When in the for loop THORRIN is called it is passed a single parameter
i.  So when substituting it x is equal to i.  This is where the problem
occurs.  The preprocessor treates i as just another piece of text, but
what you want to happen is for it to fetch the *value* of i.  The
difficulty is that the value of i is only known at run-time.

The best solution I can suggest to your problem would be to use an array
of values.

| char * thorrin[MAX_THORRIN];
| ..
|
| for (i=0; i < MAX_THORRIN; i++) {
|    printf("Address for thorrin %d is %x\n",i, &thorrin[i]);
| }

NB: &thorrin[i] can also be written thorrin + i.

Quote:
> Thanks!

> -Thorrin

> "You better practice if you wanna beat me!"
>      -Ryudo, Grandia II

--

--



Fri, 21 Jan 2005 20:54:28 GMT  
 Grrr... preprocessor and concatenation blues

Quote:

> I am trying to use c's macros to do concatenation.  I have some code like this:

> #define THORRIN(x) &thorrin ## x

> char * thorrin1;
> char * thorrin2;
> char * thorrin3;
> ..

> for (i=0; i < MAX_THORRIN; i++) {
>    printf("Address for thorrin %d is %x\n",i, THORRIN(i));
> }

> This works great if I put in THORRIN(1)... But if I put in THORRIN(i), I get an
> error message like...'thorrini not defined'

> How can I substitute the variable in the token name?  I've tried a bunch of
> combinations including 'stringing' #defines together, but I cant get that
> variable to substitue for a real value.  Any ideas?

This won't work.  The problem here is that you're trying to mix the
compiler and the preprocessor togeths, when the two actually run one
after the other, only passing processed text between them.

The easiest way to explain the problem here is to imagine that you're
the preprocessor and to step through the substitutions it makes.

Before the compiler processes your program the preprocessor runs.  On
modern compilers the preprocessor tends to be integrated into the
compiler front end, but historically the preprocessor has often been in
a separate executable, separate from the compiler.  This means that when
the preprocessor runs it knows nothing about the actual structure of
your program because it hasn't been parsed yet.

First you define the macro THORRIN.  So the preprocessor stores this
away.  To it THORRIN is a macro that takes a single parameter which is
concatenated onto &thorrin.  Its important to know here that the
preprocessor treats x as text, because all the preprocessor handles is
text.

When in the for loop THORRIN is called it is passed a single parameter
i.  So when substituting it x is equal to i.  This is where the problem
occurs.  The preprocessor treates i as just another piece of text, but
what you want to happen is for it to fetch the *value* of i.  The
difficulty is that the value of i is only known at run-time.

The best solution I can suggest to your problem would be to use an array
of values.

| char * thorrin[MAX_THORRIN];
| ..
|
| for (i=0; i < MAX_THORRIN; i++) {
|    printf("Address for thorrin %d is %x\n",i, &thorrin[i]);
| }

NB: &thorrin[i] can also be written thorrin + i.

Quote:
> Thanks!

> -Thorrin

> "You better practice if you wanna beat me!"
>      -Ryudo, Grandia II

--

--



Fri, 21 Jan 2005 20:54:30 GMT  
 Grrr... preprocessor and concatenation blues
On 03 Aug 2002 03:45:41 GMT, Thorrin said:

Quote:
> #define THORRIN(x) &thorrin ## x
>    printf("Address for thorrin %d is %x\n",i, THORRIN(i));

> This works great if I put in THORRIN(1)... But if I put in THORRIN(i), I get an
> error message like...'thorrini not defined'

You've kind of answered your question in the subject line - the
macro replacement is done at the pre-processor stage. At that
stage, there's not really any thought of the value or type of the
variable - it's basically string replacement. After your macro
gets replaced, you have &thorrin ## i in the printf, and that
gets concatenated (by the preprocessor) to &thorrini, which is
what you've observed.

Quote:
> How can I substitute the variable in the token name?  I've tried a bunch of
> combinations including 'stringing' #defines together, but I cant get that
> variable to substitue for a real value.  Any ideas?

I honestly don't know if you can dynamically generate object
names in C. I suspect you can't. Would you lose a lot by using an
array instead?

Cheers,
Dave.

--
           David Neary,
     E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html
--



Fri, 21 Jan 2005 20:54:48 GMT  
 Grrr... preprocessor and concatenation blues

Quote:
>Greetings,

>I looked in the FAQ and searched on deja.  I found a million things that were
>close to what Im trying to do, but cant find the exact thing.  Any help on this
>will be appreciated!

>I am trying to use c's macros to do concatenation.  I have some code like this:

>#define THORRIN(x) &thorrin ## x

>char * thorrin1;
>char * thorrin2;
>char * thorrin3;
>..

>for (i=0; i < MAX_THORRIN; i++) {
>   printf("Address for thorrin %d is %x\n",i, THORRIN(i));
>}

>This works great if I put in THORRIN(1)... But if I put in THORRIN(i), I get an
>error message like...'thorrini not defined'

>How can I substitute the variable in the token name?  I've tried a bunch of
>combinations including 'stringing' #defines together, but I cant get that
>variable to substitue for a real value.  Any ideas?

You can't mix compile time generation and run time variables like
this. If you want to use a run time variable index, you need to
use an array, otherwise you need to repeat the macro umpty times
with a constant and suitable guard code.

if (0 < MAX_THORRIN) printf("Address for thorrin0 is %x\n",
THORRIN(0));
....

which suggests using another macro:

#define PRINT_THORRIN(x) if (x < MAX_THORRIN) \
        {printf("Address for thorrin"#x" is %x\n",&thorrin##x);}

used as:

PRINT_THORRIN(0)
PRINT_THORRIN(1)
PRINT_THORRIN(2)
PRINT_THORRIN(3)
PRINT_THORRIN(4)
PRINT_THORRIN(5)
PRINT_THORRIN(6)
PRINT_THORRIN(7)
PRINT_THORRIN(8)
PRINT_THORRIN(9)
....

--

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada


    fake address                use address above to reply


                                                spam traps
--



Fri, 21 Jan 2005 20:55:15 GMT  
 Grrr... preprocessor and concatenation blues

Quote:
> Greetings,

> I looked in the FAQ and searched on deja.  I found a million things
that were
> close to what Im trying to do, but cant find the exact thing.  Any
help on this
> will be appreciated!

> I am trying to use c's macros to do concatenation.  I have some code
like this:

> #define THORRIN(x) &thorrin ## x

> char * thorrin1;
> char * thorrin2;
> char * thorrin3;
> ..

> for (i=0; i < MAX_THORRIN; i++) {
>    printf("Address for thorrin %d is %x\n",i, THORRIN(i));
> }

You should use %p to print pointer values, and you should also cast to
(void *) to be absolutely safe, since that's the type of value %p
expects.

Quote:
> This works great if I put in THORRIN(1)... But if I put in THORRIN(i),
I get an
> error message like...'thorrini not defined'

> How can I substitute the variable in the token name?  I've tried a
bunch of
> combinations including 'stringing' #defines together, but I cant get
that
> variable to substitue for a real value.  Any ideas?

There's no way you can do it with a loop like that; the variable i gets
its value at runtime, so the preprocessor clearly can't see the value at
compile time.  There are no loop constructs in the preprocessor, so
you'll have to resort to one macro call per variable, I'm afraid.  The
only other alternative would be using some separate text processor to
generate the source lines automatically and feeding the result to the
compiler; your choice will depend a lot on your OS and environment.

--
Eric Amick
Columbia, MD
--



Fri, 21 Jan 2005 20:55:17 GMT  
 Grrr... preprocessor and concatenation blues

Quote:
>Greetings,

>I looked in the FAQ and searched on deja.  I found a million things that were
>close to what Im trying to do, but cant find the exact thing.  Any help on this
>will be appreciated!

>I am trying to use c's macros to do concatenation.  I have some code like this:

>#define THORRIN(x) &thorrin ## x

>char * thorrin1;
>char * thorrin2;
>char * thorrin3;
>..

>for (i=0; i < MAX_THORRIN; i++) {
>   printf("Address for thorrin %d is %x\n",i, THORRIN(i));
>}

>This works great if I put in THORRIN(1)... But if I put in THORRIN(i), I get an
>error message like...'thorrini not defined'

>How can I substitute the variable in the token name?  I've tried a bunch of
>combinations including 'stringing' #defines together, but I cant get that
>variable to substitue for a real value.  Any ideas?

Since the preprocessor runs long before any values are stored in i, it
can only do character substitution, not value substitution.  For what
you describe, try

        char *thorrin[3];
        printf(....,thorrin[i]).

<<Remove the del for email>>
--



Fri, 21 Jan 2005 20:55:44 GMT  
 Grrr... preprocessor and concatenation blues
What you're trying to do can't work.  The preprocessor replaces the
#define statements at compile time, but the variable i only takes a
value at run time.  Therefore the preprocessor can't act on the value
of i.

Instead you should use an array, with something like

char * thorrin[MAX_THORRIN];

for (i=0; i < MAX_THORRIN; i++) {
    printf("Address for thorrin[%d] is %x\n", i, &thorrin[i]);

Quote:
}

- Larry

Quote:

> Greetings,

> I looked in the FAQ and searched on deja.  I found a million things that were
> close to what Im trying to do, but cant find the exact thing.  Any help on this
> will be appreciated!

> I am trying to use c's macros to do concatenation.  I have some code like this:

> #define THORRIN(x) &thorrin ## x

> char * thorrin1;
> char * thorrin2;
> char * thorrin3;
> ..

> for (i=0; i < MAX_THORRIN; i++) {
>    printf("Address for thorrin %d is %x\n",i, THORRIN(i));
> }

> This works great if I put in THORRIN(1)... But if I put in THORRIN(i), I get an
> error message like...'thorrini not defined'

> How can I substitute the variable in the token name?  I've tried a bunch of
> combinations including 'stringing' #defines together, but I cant get that
> variable to substitue for a real value.  Any ideas?

> Thanks!

> -Thorrin

> "You better practice if you wanna beat me!"
>      -Ryudo, Grandia II

--



Fri, 21 Jan 2005 20:56:02 GMT  
 Grrr... preprocessor and concatenation blues

Quote:

>This works great if I put in THORRIN(1)... But if I put in THORRIN(i), I get an
>error message like...'thorrini not defined'

Because you're creating a token named "thorrini".

Quote:
>How can I substitute the variable in the token name?  I've tried a bunch of

You can't.  The for-loop is executed, and the variable is
compiled-in by then.

Quote:
>combinations including 'stringing' #defines together, but I cant get that
>variable to substitue for a real value.  Any ideas?

Use an array.

                                --Blair
                                  "Unless you got another requirement that
                                   you're holding out on us."
--



Fri, 21 Jan 2005 20:56:27 GMT  
 Grrr... preprocessor and concatenation blues
[...]

Quote:
> for (i=0; i < MAX_THORRIN; i++) {
>    printf("Address for thorrin %d is %x\n",i, THORRIN(i));
> }

This cannot ever work.  C preprocessor macros are all handled at
compile time, but you're trying to access them at run time of the
program.  At the time the preprocessor does its work, 'i' doesn't
exist yet, let alone have one of the proposed values.

A hint: if you wanted an array, use one.
--

Even if all the snow were burnt, ashes would remain.
--



Fri, 21 Jan 2005 20:56:55 GMT  
 Grrr... preprocessor and concatenation blues

[...]

Quote:
>#define THORRIN(x) &thorrin ## x

>char * thorrin1;
>char * thorrin2;
>char * thorrin3;
>..

>for (i=0; i < MAX_THORRIN; i++) {
>   printf("Address for thorrin %d is %x\n",i, THORRIN(i));
>}
[...]
>How can I substitute the variable in the token name?

You can't.  Not the way you want.  Not with the preprocessor anyway.
The preprocessor does simple text substitution, and has no irep.

Try this:

  char **thorrin_adr[] = {NULL, &thorrin1, &thorrin2, &thorrin3};

  for (i=1; i<4; i++)
    printf("thorrin%d is %p\n",i,*thorrin_adr[i]);

Regards,

                               -=Dave
--
Change is inevitable, progress is not.
--



Sat, 22 Jan 2005 04:29:49 GMT  
 Grrr... preprocessor and concatenation blues

Quote:

> Greetings,

> I looked in the FAQ and searched on deja.  I found a million things that were
> close to what Im trying to do, but cant find the exact thing.  Any help on this
> will be appreciated!

> I am trying to use c's macros to do concatenation.  I have some code like this:

> #define THORRIN(x) &thorrin ## x

> char * thorrin1;
> char * thorrin2;
> char * thorrin3;
> ..

> for (i=0; i < MAX_THORRIN; i++) {
>    printf("Address for thorrin %d is %x\n",i, THORRIN(i));
> }

> This works great if I put in THORRIN(1)... But if I put in THORRIN(i), I get an
> error message like...'thorrini not defined'

> How can I substitute the variable in the token name?  I've tried a bunch of
> combinations including 'stringing' #defines together, but I cant get that
> variable to substitue for a real value.  Any ideas?

> Thanks!

> -Thorrin

> "You better practice if you wanna beat me!"
>      -Ryudo, Grandia II

Macro are compile time entities. You cannot use them to do a  runtime job.

- Uma Mohan Reemala
--



Sat, 22 Jan 2005 04:29:57 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. creating a variable name via preprocessor concatenation

2. Preprocessor #define concatenation

3. Preprocessor blues

4. Blue screen blues

5. Blue on Blue

6. Blue on Blue II

7. Grrr... Conditionally formatting a DataGrid (not ASP)

8. Unresolved type -- _Recordset -- GRRR!

9. RPC Expert coding - RAS Dialog Appear (Grrr....)

10. ListBox in Modal dialog (Grrr)

11. VARIANTTYPE -grrr

12. String Concatenation

 

 
Powered by phpBB® Forum Software