multiline macro with embedded #

H

Hal Styli

Hello, can someone please help with the following attempt at defining a
macro ...

I want to wrap the following in a macro:-

if HEAD is defined then
head=p;
define HEAD
else
tail->next=p;
end
tail=p;

Here is the attemp:-

#define PTRS \
#ifndef HEAD \
head=p; \
#define HEAD 1 \
#else \
tail->next=p; \
#endif \
tail=p;

Predictably this comes out as: #ifndef HEAD head=p; #define HEAD 1 #else
tail->next=p; #endif tail=p;
I don't thisk this is compiler specific but just for the record,
Borland BCC give me .. Illegal character '#' (0x23) in function main

I'm hoping it is possible to accomplish what I want to do with macros.

Thanks in advance for any help given.

Hal.
 
A

Artie Gold

Hal said:
Hello, can someone please help with the following attempt at defining a
macro ...

I want to wrap the following in a macro:-

if HEAD is defined then
head=p;
define HEAD
else
tail->next=p;
end
tail=p;

Here is the attemp:-

#define PTRS \
#ifndef HEAD \
head=p; \
#define HEAD 1 \
#else \
tail->next=p; \
#endif \
tail=p;

Predictably this comes out as: #ifndef HEAD head=p; #define HEAD 1 #else
tail->next=p; #endif tail=p;
I don't thisk this is compiler specific but just for the record,
Borland BCC give me .. Illegal character '#' (0x23) in function main

I'm hoping it is possible to accomplish what I want to do with macros.
You can, but not that way (i.e. the C preprocessor does not allow
macro-defining macros).

Fortunately, what you want isn't particularly hard.

#ifndef HEAD
#define HEAD 1
#define PTRS head=p; tail=p;
#else
#define PTRS tail->next=p; tail=p;
#endif

*Unfortunately*, this doesn't seem to make a whole lot of sense either
practically or stylistically. What is it that you're trying to do? Could
it be the case (quite likely) that using macros here is not the best way?

HTH,
--ag
 
B

Ben Pfaff

Hal Styli said:
I want to wrap the following in a macro:-

if HEAD is defined then
head=p;
define HEAD
else
tail->next=p;
end
tail=p;

You can't do that. Here is what C99 6.10.3.4 says about
preprocessing directives resulting from macro expansion.

3 The resulting completely macro-replaced preprocessing token
sequence is not processed as a preprocessing directive even
if it resembles one, but all pragma unary operator
expressions within it are then processed as specified in
6.10.9 below.
 
H

Hal Styli

Artie Gold said:
Hal Styli wrote:

Fortunately, what you want isn't particularly hard.

#ifndef HEAD
#define HEAD 1
#define PTRS head=p; tail=p;
#else
#define PTRS tail->next=p; tail=p;
#endif

I wanted to use PTRS in another macro and invoke that macro several times.
This won't do it but it seems no macro method will given your comments and
Ben's.
*Unfortunately*, this doesn't seem to make a whole lot of sense either
practically or stylistically. What is it that you're trying to do? Could
it be the case (quite likely) that using macros here is not the best way?

Could be! I was pushing the limits a bit. Guess I'll revert to a function of
some sort.

Thank you both for your swift responses.
 
E

Ed Morton

Hal Styli wrote:
#define PTRS \
#ifndef HEAD \
head=p; \
#define HEAD 1 \
#else \
tail->next=p; \
#endif \
tail=p;

Predictably this comes out as: #ifndef HEAD head=p; #define HEAD 1 #else
tail->next=p; #endif tail=p;
I don't thisk this is compiler specific but just for the record,
Borland BCC give me .. Illegal character '#' (0x23) in function main

I'm hoping it is possible to accomplish what I want to do with macros.

Thanks in advance for any help given.

Assuming that you're just trying to populate a list and need to know
dynamically if the list already has a head populated, you need to use a
variable rather than trying to use a #define for "HEAD", e.g.:

#define PTRS \
do { \
if (head == NULL) { \
head = p; \
} else { \
tail->next = p; \
} \
tail = p; \
while(0)

Obviously you need to make sure you init "head" to NULL before using
this macro. Passing some parameters might not be a bad idea either ;-).
If you post a more complete code segment, you'll probably get some
better suggestions on how to accomplish what you really want.

Regards,

Ed.
 
E

Eric Sosman

Hal said:
Hello, can someone please help with the following attempt at defining a
macro ...
[...]
#define PTRS \
#ifndef HEAD \
head=p; \
#define HEAD 1 \
#else \
tail->next=p; \
#endif \
tail=p;

Predictably this comes out as: #ifndef HEAD head=p; #define HEAD 1 #else
tail->next=p; #endif tail=p;
I don't thisk this is compiler specific but just for the record,
Borland BCC give me .. Illegal character '#' (0x23) in function main

I'm hoping it is possible to accomplish what I want to do with macros.

This is Question 10.14 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/faq.html
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top