0

I want to know how preprocessor directives are evaluated, when they are placed in a loop in C/C++? Following is the code, which uses directive in a for loop, but it doesn't work. Why is it so?

main.cpp

#include <stdio.h>
class Student
{
public:
    int roll;
    int marks;

    Student()
    {
        roll = 10;
        marks = 0;
    }
};    

int main()
{
    printf("Hello, World!\n");
    int iCounter;

    char attr[][6] = {"roll", "marks"};

    Student std;

#define PRINT1(std, X) printf("%d", std.##X);
    for (iCounter = 0; iCounter < 2; iCounter++)
    {
        PRINT1(std, attr[iCounter])
    }

    return 0;
}
4
  • Why should it? attr is no member of class Student. Commented Jul 17, 2015 at 10:49
  • 2
    [devil's advocate for Q&A quality purposes, because in reality I can guess what you mean here] How are we supposed to know your definition of "working" by only reading code that, by your own admission, does not exhibit a valid implementation of that definition? And why is this tagged C? Commented Jul 17, 2015 at 10:50
  • 1
    I think you need to read a book and understand how C++ and the preprocessor work, because this is not at all valid code. Commented Jul 17, 2015 at 10:51
  • 3
    "it doesn't work" is not a good description of a problem. Please tell us what behavior you experience (compile/runtime error?). Also: please do not use std as a variable name, because this is also a standard namespace. Commented Jul 17, 2015 at 10:51

1 Answer 1

1

The preprocessor is a pre-compilation phase. It is essentially a code generator and, as such, has no knowledge of or interaction with your for loops.

You simply cannot iterate over a class's members in this manner.

Here's what your preprocessed code looks like (I've omitted header expansions for obvious reasons):

class Student
{
public:
    int roll;
    int marks;

    Student()
    {
        roll = 10;
        marks = 0;
    }
};    

int main()
{
    printf("Hello, World!\n");
    int iCounter;

    char attr[][6] = {"roll", "marks"};

    Student std;

    for (iCounter = 0; iCounter < 2; iCounter++)
    {
        printf("%d", std.attr[iCounter]);
    }

    return 0;
}

As attr is not a member of std (terrible name for an object; please change it), hopefully you can now see how this is not going to "work" as you intend.

Furthermore, your macro is actually inherently broken and produces an error, because you are using ## where you shouldn't. Even if this code were what you wanted, the proper definition would be simply:

#define PRINT1(std, X) printf("%d", std.X);
Sign up to request clarification or add additional context in comments.

2 Comments

when "##" can be used ?
@user2393267 gcc.gnu.org/onlinedocs/cpp/Concatenation.html#Concatenation It's used to concatenate two tokens into one token (e.g. ch ## ar -> char). You're not trying to concatenate anything; just putting two tokens next to each other is not concatenation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.