Solved

Joining and expanding #defines

Posted on 2004-08-04
8
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

710 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