Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

c - Is there a way to use a char array/variable in #include

I want to use a variable as input for the #include statement, but i have to use the quotation marks which converts my variable to a simple string.

Is there a way to prevent this?

The code looks like this

void toObjectArray(char file_name[]){
  struct telefonbuchEintrag tListe[]=
  {
    #include file_name
  };
}

And i have to use quotation marks, is there a way to add them without changing my variable to a string?

question from:https://stackoverflow.com/questions/65859364/is-there-a-way-to-use-a-char-array-variable-in-include

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you are trying to do won’t work - #include directives are not processed at runtime, but during the pre-processing phase of compilation. IOW, you can’t execute an #include directive in a running program.

The question is why you want to do this - what problem are you trying to solve? If you honestly have a situation where you to choose between multiple headers based on some condition, then you’ll have to write a program to generate that code and then compile the generated code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...