George
George

Reputation: 52

Get string from plain text through define

So for a task Im trying to to something like this:

#define GET_STR 
#define OK

and use it like this:

GET_STR sample_string OK

It should assign to a string the -sample_string- value; Tried to define GET_STR with enum types, or just use the '#' but did not seem to work. If the define was type GET_STR(str) I could just use #str and then assign to a string. Any ideas?

Upvotes: 0

Views: 563

Answers (2)

This appears to work:

#include <stdio.h>

#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#define GET_STR STRINGIFY(
#define OK )

int main()
{
    printf(GET_STR hello world OK);
    printf("\n");
    return 0;
}

Upvotes: 1

Jts
Jts

Reputation: 3527

What about this?

#define GET_STR std::string s(
#define OK ); 

std::string sample_string = "hello";
GET_STR sample_string OK

or

GET_STR "sample_stringggg" OK 

Upvotes: 1

Related Questions