##-concatenation

R

Ravi Uday

Hi,

Why is this code not working ?
It says " 'TestFn_this_is_new_string' : undeclared identifier"

#define NEW_STR(x) {x##_this_is_new_string }

struct command
{
char *name;
}cm[10] = {NEW_STR(testFn)} ;


Thx,
Ravi
 
C

Christopher Benson-Manica

Ravi Uday said:
Why is this code not working ?
It says " 'TestFn_this_is_new_string' : undeclared identifier"
#define NEW_STR(x) {x##_this_is_new_string }
struct command
{
char *name;
}cm[10] = {NEW_STR(testFn)} ;

Well, after the macro substitution, you get

struct command
{
char *name;
} cm[10] = {testFn_this_is_new_string} ;

First, note that the error message you posted has a capital T, while
the string you're using for the macro argument has a lowercase t. C
is case-sensitive, so that could be your problem right there. If not,
it's probably as Joona suggested - you just didn't declare this
oddly-named variable anywhere.
 
R

Richard Delorme

Ravi Uday a écrit :
Why is this code not working ?
It says " 'TestFn_this_is_new_string' : undeclared identifier"

#define NEW_STR(x) {x##_this_is_new_string }

I think you want this instead:

#define NEW_STR(x) {#x "_this_is_new_string" }

The following points can explain why the above code works, but not yours:
- You forgot the quotes to make a valid literal string.
- You did not use the '#' to turn an identifier into a literal string.
- Consecutive literal strings are automatically concatenated, so you do
not need the ## macro operator.
struct command
{
char *name;
}cm[10] = {NEW_STR(testFn)} ;
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top