macros: not a valid preprocessing token

C

Chris

Hi all

We have a strange problem with macros:

#define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[ ##Tbl##_##Fld]=i##Tbl##_##Fld;

SQLST_MAP_IND_I(RAGREEJ1,FORMFROMTMSTP);

=> gcc 3.3.4 gives the following error:
source.c:173:40: pasting "[" and "RAGREEJ1" does not give a valid
preprocessing token


The same code compiles fine on Win32 (MSVC 7.1) and on z/OS

So what is wrong? The code or... the precompiler?

Bye
Chris
 
R

Russell Shaw

Chris said:
Hi all

We have a strange problem with macros:

#define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[ ##Tbl##_##Fld]=i##Tbl##_##Fld;

SQLST_MAP_IND_I(RAGREEJ1,FORMFROMTMSTP);

=> gcc 3.3.4 gives the following error:
source.c:173:40: pasting "[" and "RAGREEJ1" does not give a valid
preprocessing token


The same code compiles fine on Win32 (MSVC 7.1) and on z/OS

So what is wrong? The code or... the precompiler?

Try: #define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[Tbl##_##Fld]=i##Tbl##_##Fld
 
S

SM Ryan

# Hi all
#
# We have a strange problem with macros:
#
# #define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[ ##Tbl##_##Fld]=i##Tbl##_##Fld;
#
# SQLST_MAP_IND_I(RAGREEJ1,FORMFROMTMSTP);
#
# => gcc 3.3.4 gives the following error:
# source.c:173:40: pasting "[" and "RAGREEJ1" does not give a valid
# preprocessing token

You only need to paste together pieces that become a single identifier or
number or other single lexical entity. Assuming there is nothing missing
between the [ and ##, you don't need to paste the [ and Tbl parameter
together.

# The same code compiles fine on Win32 (MSVC 7.1) and on z/OS
#
# So what is wrong? The code or... the precompiler?

Possibly one compiler reanalyzes the characters of a #define expansion
and the other does not.
 
A

Arthur J. O'Dwyer

We have a strange problem with macros:

#define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[ ##Tbl##_##Fld]=i##Tbl##_##Fld;

SQLST_MAP_IND_I(RAGREEJ1,FORMFROMTMSTP);

=> gcc 3.3.4 gives the following error:
source.c:173:40: pasting "[" and "RAGREEJ1" does not give a valid
preprocessing token

You've gotten two "correct but [almost] useless" answers so far, IMHO.
To summarize and elucidate: Your code is wrong. You are trying to paste
the strings '[' and 'RAGREEJ1', giving the token '[RAGREEJ1'. That's
not a valid token in C, so the compiler correctly diagnoses the error.
Your Microsoft and z/OS (whatever that is) compilers are broken,
or you're running them in non-conforming mode. Try turning up the
warning levels.

How to fix this problem: Simple. As has already been pointed out,
you don't want the token '[RAGREEJ1'. You want two tokens. So just
don't paste them together, and they'll stay two tokens!

Wrong:
#define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[ ##Tbl##_##Fld]=i##Tbl##_##Fld;

Right:
#define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[Tbl##_##Fld]=i##Tbl##_##Fld;

Note the removal of the erroneous '##' between '[' and 'Tbl'.

HTH,
-Arthur
 
C

Chris

Alright, thanks alot!

Arthur J. O'Dwyer said:
We have a strange problem with macros:

#define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[ ##Tbl##_##Fld]=i##Tbl##_##Fld;

SQLST_MAP_IND_I(RAGREEJ1,FORMFROMTMSTP);

=> gcc 3.3.4 gives the following error:
source.c:173:40: pasting "[" and "RAGREEJ1" does not give a valid
preprocessing token

You've gotten two "correct but [almost] useless" answers so far, IMHO.
To summarize and elucidate: Your code is wrong. You are trying to paste
the strings '[' and 'RAGREEJ1', giving the token '[RAGREEJ1'. That's
not a valid token in C, so the compiler correctly diagnoses the error.
Your Microsoft and z/OS (whatever that is) compilers are broken,
or you're running them in non-conforming mode. Try turning up the
warning levels.

How to fix this problem: Simple. As has already been pointed out,
you don't want the token '[RAGREEJ1'. You want two tokens. So just
don't paste them together, and they'll stay two tokens!

Wrong:
#define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[
##Tbl##_##Fld]=i##Tbl##_##Fld;

Right:
#define SQLST_MAP_IND_O(Tbl,Fld) i##Tbl##_O[Tbl##_##Fld]=i##Tbl##_##Fld;

Note the removal of the erroneous '##' between '[' and 'Tbl'.

HTH,
-Arthur
 
C

Chris Torek

You've gotten two "correct but [almost] useless" answers so far, IMHO.
To summarize and elucidate: Your code is wrong. You are trying to paste
the strings '[' and 'RAGREEJ1', giving the token '[RAGREEJ1'. That's
not a valid token in C, so the compiler correctly diagnoses the error.

Actually, I think one of the previous answers was useful. :)

This is indeed the problem (that the ## token-pasting operator is
being used inappropriately). However:
Your Microsoft and z/OS (whatever that is) compilers are broken,
or you're running them in non-conforming mode. Try turning up the
warning levels.

These compilers are not actually *broken* (or at least, not provably
so from this example). If two pp-tokens pasted with "##" do not
form a valid new pp-token, the behavior is undefined, and no
diagnostic is required. The effect of that undefined behavior can
even be "the compiler does what you wanted after all".

Still, Arthur's fix is correct, and the new code is guaranteed to
work, rather than depending on compiler behavior.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top