473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

multiline macro with embedded #

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.
Nov 14 '05 #1
5 2731
Hal Styli wrote:
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

--
Artie Gold -- Austin, Texas
Nov 14 '05 #2
"Hal Styli" <no_spam@all> writes:
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.

--
"Am I missing something?"
--Dan Pop
Nov 14 '05 #3
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:c5************@ID-219787.news.uni-berlin.de...
Hal Styli wrote:
<<pruned>>

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.
Nov 14 '05 #4


Hal Styli wrote:
<snip>
#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.

Nov 14 '05 #5
Hal Styli wrote:

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

--
Er*********@sun.com
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rasmus Fogh | last post by:
Dear All, I need a way of writing strings or arbitrary Python code that will a) allow the strings to be read again unchanged (like repr) b) write multiline strings as multiline strings instead...
0
by: Hal Styli | last post by:
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;
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
8
by: Konrad | last post by:
Hello, Stupid question, but I just don't remember, here is what I want to do: #ifdef _UNICODE #define ONLY_UNICODE(t) t #else #define ONLY_UNICODE(t) #endif You know what it does, but I'm...
3
by: Masa Ito | last post by:
I am trying to capture the contents of a function with Regex. I am using Expresso to test (nice - thanks for the great tool UltraPico!). I can handle my own with single line regex's (I think).. ...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
9
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
I am replacing a number of functions with macros. Some of these functions "do something" and then return a variable. This behaviour I have found that I can emulate using the comma operator: ...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
4
by: saswatdash83 | last post by:
Hi All, I want to implement a macro which will toggle a bit in a byte.The user will give input from keyboard. toggle(number,bit to be toggled) Number,bit to be toggled will be input from...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.