Query about " warning parameter names (without types) in functiondeclaration"

P

prix prad

Hi All,
I encountered the above error when I tried to expand a macro
as follows:

#define EXPAND(array) int M_ ## array

The problem occurs when we have 'array' itself as a macro:

say EXPAND(ARR(DECL))
where #define DECL _decl
#define ARR(name) arr ## name

So the end result would be M_arr_name

The error goes away when I do:

#define EXPAND(array) int PREPEND_ARR(array)
where
PREPEND_ARR(array) M_ ## array

Can someone explain me why the error is about a 'function declaration'
in this case since we are dealing with MACROS?

Thanks,
/prix
 
J

Jack Klein

Hi All,
I encountered the above error when I tried to expand a macro
as follows:

#define EXPAND(array) int M_ ## array

The problem occurs when we have 'array' itself as a macro:

say EXPAND(ARR(DECL))

"say" what?
where #define DECL _decl
#define ARR(name) arr ## name

The end result of what, exactly?
So the end result would be M_arr_name

The error goes away when I do:

#define EXPAND(array) int PREPEND_ARR(array)
where
PREPEND_ARR(array) M_ ## array

Can someone explain me why the error is about a 'function declaration'
in this case since we are dealing with MACROS?

Have you put your macros together in a minimal file and asked your
compiler to show you its preprocessing output? Virtually all
compilers provide this feature?

If so, post the exact contents of that minimal file, all of about
three lines, exactly. Don't use words to tell us about it, show us
the actual macro definitions and expansions. And the output of your
compiler's preprocessor.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
A

Andrey Tarasevich

prix said:
I encountered the above error when I tried to expand a macro
as follows:

#define EXPAND(array) int M_ ## array

The problem occurs when we have 'array' itself as a macro:

say EXPAND(ARR(DECL))
where #define DECL _decl
#define ARR(name) arr ## name

So the end result would be M_arr_name

The error goes away when I do:

#define EXPAND(array) int PREPEND_ARR(array)
where
PREPEND_ARR(array) M_ ## array

Can someone explain me why the error is about a 'function declaration'
in this case since we are dealing with MACROS?
...

C preprocessor does not perform preliminary macro expansion for arguments
adjacent to ## operator. For this reason in your first variant of EXPAND the
argument ARR(DECL) is not expanded and

EXPAND(ARR(DECL))

first turns into

int M_ARR(DECL)

and later, after rescanning into the end result

int M_ARR(_decl)

Since 'ARR' get concatenated with 'M_', the preprocessor never gets a chance to
identify that 'ARR' with your 'ARR' macro.

This final result looks like a function declaration to the compiler, which is
why it complains about function declaration.

In your second variant of EXPAND theres no ## in EXPAND and for this reason in

EXPAND(ARR(DECL))

the preliminary macro expansion is applied to the argument ARR(DECL), resulting in

int PREPEND_ARR(arr_decl)

the further rescan turns this into

int M_arr_decl
 
P

prix prad

Hi Andrey,
Thanks for the lucid explanation.
Hi Jack,

The following is the .c file:
********************************************************************
#define DECL _decl
#define ARR(name) arr ## name
#define EXPAND(array) int M_ ## array

main() {
EXPAND(ARR(DECL));
return 1;
}
********************************************************************

The following is the 'gcc -E' output:
********************************************************************
main() {
int M_ARR(_decl);
return 1;
}
********************************************************************

Thanks,
/prix
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top