Solved

Joining and expanding #defines

Posted on 2004-08-04
8
241 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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 while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now