Solved

Joining and expanding #defines

Posted on 2004-08-04
8
245 Views
Last Modified: 2010-04-15
Fundamentally, I'd like to join two #defined constants to form a new constant, and use the value of that third constant in a subsequent operation.

For example, something like:

#define one apple
#define two pie
#define applepie 0x12345678

i=*onetwo

and i should end up with the value in location 0x12345678
0
Comment
Question by:rtsh
  • 3
  • 3
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11713577
Hi rtsh,

you can do that in effect but not like that

int i =strlen(one);
i += strlen(two);

char * a = (char *)calloc (i+1);
strcpy (a,one);
strcat(a,two);

now you can use a as a concatenated value

Sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11713580
rtsh,

> and i should end up with the value in location 0x12345678
that may not be necessarily possible ... you can try to write to that location but most likely you will end up getting a segfault

Sunnycoder
0
 
LVL 1

Author Comment

by:rtsh
ID: 11713618
Sunnycoder:

Re the segfault - I'm well aware... was just an example.  

I really don't like the strcpy method - there must be a way to do this preprocessor?

I can join defines with ##, but I can't seem to force the order so that they're evaluated prior to the catenation - I'd just get onetwo, which doesn't exist...
0
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
LVL 9

Expert Comment

by:ankuratvb
ID: 11714533
Hi rtsh,

Using the preprocessor,you can't do that.

>but I can't seem to force the order so that they're evaluated prior to the catenation - I'd >just get onetwo, which doesn't exist

Precisely why.

#defines cannot be forced to evaluate before the substitution.
0
 
LVL 1

Author Comment

by:rtsh
ID: 11714543
I've finally found the answer - see:

http://tigcc.ticalc.org/doc/cpp.html#SEC28 which says:

"Macros that call other macros that stringify or concatenate:

If an argument is stringified or concatenated, the prescan does not occur. If you want to expand a macro, then stringify or concatenate its expansion, you can do that by causing one macro to call another macro that does the stringification or concatenation. For instance, if you have

#define AFTERX(x) X_ ## x
#define XAFTERX(x) AFTERX(x)
#define TABLESIZE 1024
#define BUFSIZE TABLESIZE

then AFTERX(BUFSIZE) expands to X_BUFSIZE, and XAFTERX(BUFSIZE) expands to X_1024. (Not to X_TABLESIZE. Prescan always does a complete expansion.) "

0
 
LVL 1

Author Comment

by:rtsh
ID: 11714552
(Can an admin close this now I've found the answer?)
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 0 total points
ID: 11750168
Question PAQed and 350 points refunded

sunnycoder
Page Editor
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stack 22 172
Problem to save 10 174
delete-remove 14 97
Test the speeds on my PC Drives 12 63
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question