Is it possible to create C preprocessor macro that would allow me to call methods on strings(char[]).
e.g.:
char myStr[10];
int len = strlen(myStr);
Would become:
char myStr[10];
int len = myStr.len;
or:
char myStr[10];
int len = LEN myStr;
Edit: Just to clarify, this question is about C-preprocessor not C language itself. For all intents and purposes, the code above could be replaced with JavaScript, PHP, or even plain text.
All I am asking is, whether is it possible to write macro that would wrap text before token or after token with some other text.
Generally speaking, the following transformation:
token text => wrap text wrap
or
text token => wrap text wrap
CodePudding user response:
No, the preprocessor cannot do this:
token text => wrap text wrap
or
text token => wrap text wrap
In simple words, its replacing capabilities include only, and the token needs to be an identifier:
token => replacement
token '(' argument(s) ')' => replacement with 0 to all of _argument(s)
See the chapter 6.10.3 of the standard.
You might want to do some research on other macro processors, for example starting on the respective Wikipedia page. Please note that recommendations are off-topic here on StackOverflow.
CodePudding user response:
#define strlen(str) str.len
int foo(char *str)
{
size_t x = strlen(str);
}