Public Group

# Preprocessor Directive Macro

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The following code doesn't work: #include <stdlib.h> #include <stdio.h> #define paster(n) n struct data { int num1; int num2; }; void main() { data s1; char *szField = "num1"; s1.num1 = 0; s1.num2 = 0; printf("s1.num1 = %d", s1.paster(szField)); } I want my paster function to paste the value of szField, not the letters szField themselves. Is there a way to do this using preprocessor directives?

##### Share on other sites
Does this even compile?

Also, this sounds like a homework question which we aren't supposed to answer. I can however give you a hint.

It sounds like what you really want to do is write a macro that takes in szField, and prints its value to the screen. I would start by writing a regular function that does that, and then turn it into a macro.

To do exactly what you asked, I don't belive there is a way.

Matt Hughson

##### Share on other sites
shishio,

Preprocessor directives can sometimes be a bit confusing. Preprocessor directives are essentially an automated cut/paste that the compiler does for you before it begins compiling.

In the code example you've provided, you indicate with your #define that anywhere in your code that you have paster(n), you'd like to replace it with n. The only place that you're using the preprocessor define is on the line that says:

printf( "s1.num1 = %d", s1.paster(szField) );

The preprocessor is going to make a first pass and change the line to the following:

printf("s1.num1 = %d", s1.szField );

If this is not what you had intended, you probably need to change the macro. Based on your printf statement it looks like you're trying to output the following string to the console:

s1.num1 = 0

To do that simply use the following printf:

printf("s1.num1 = %d", s1.num1 );

What I believe you're trying to do is to take the contents of the szField variable and use it as part of the variable name passed to the printf. To my knowledge there's no way to do this, at least not in C++. But if someone knows how I'd love to hear it. =)

##### Share on other sites
Yeah the code doesn't compile. It's a poor attempt trying to illustrate what I'm trying to do. It's not a homework question... I just have a large struct with many fields. I read the field names and the field values from a file. I want to store the field values in the corresponding field name but I don't want to write a huge if-else statement to do it...

ie I don't want to do this:

if (strcmp(szField, "num1"))
{
s1.num1 = blah;
}
else if (strcmp(szField, "num2"))
{
s2.num2 = blah;
}
... etc.

I asked this before and someone told me to use the map function... but I never got it working...

Yeah I'm trying to do exactly what JWalsh said.... but if it can't be done... I better get my ctrl-c/ctrl-v ready... ;)

##### Share on other sites
shishio,

As you mentioned, you might consider using the Standard Template Library's Map class. A map is just a key-value pair. That is, for each key, there is exactly one value associated with it.

As an example, suppose you have a file with student names and scores for a high school class. The file might look something like this:

"Jeromy" 100
"Michael" 87
"Lindsey" 99
...

What you could do is read in the key (names) and values (scores) and then store them in a map such as the folling code indicates:

#include <map>#include <string>using namespace std;int _tmain(int argc, _TCHAR* argv[]){    // This is where all the code is for reading the grades in from file    // ...    std::map< std::string, int> grades;    for( int i = 0; i < numGrades; i++ )    {        grades[ name ] = grade;    }        return 0;}

As you can see from the above code, you can use the names of the students as an index into the list of grades. This is functionally the same as

##### Share on other sites
Ah crap ok I get it now... yeah the map function will work. Thanks!

• 10
• 18
• 14
• 18
• 15