Solved

Joining and expanding #defines

Posted on 2004-08-04
8
243 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using pointers to pointers to write to a two dimensional array 16 92
Socket Programming (Unix) 8 129
C Programming - If Statement 8 78
Raspberry Pi 3 to send text message 9 115
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
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 for-loops in the C programming language.

809 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