Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird define in C++ preprocessor

I've come across this

#define DsHook(a,b,c) if (!c##_) {  INT_PTR* p=b+*(INT_PTR**)a;  VirtualProtect(&c##_,4,PAGE_EXECUTE_READWRITE,&no); *(INT_PTR*)&c##_=*p;  VirtualProtect(p,4,PAGE_EXECUTE_READWRITE,&no);  *p=(INT_PTR)c; }

and everything is clear except the "c##_" word, what does that mean?

like image 833
Marco A. Avatar asked Aug 30 '26 04:08

Marco A.


2 Answers

It means to "glue" together, so c and _ get "glued together" to form c_. This glueing happens after argument replacement in the macro. See my example:

#define glue(a,b) a##_##b

const char *hello_world = "Hello, World!";

int main(int arg, char *argv[]) {
    printf("%s\n", glue(hello,world)); // prints Hello, World!
    return 0;
}
like image 67
orlp Avatar answered Sep 01 '26 18:09

orlp


It is called a token-pasting operator. Example:

// preprocessor_token_pasting.cpp
#include <stdio.h>
#define paster( n ) printf( "token" #n " = %d", token##n )
int token9 = 9;

int main()
{
   paster(9);
}

Output

token9 = 9
like image 44
Armen Tsirunyan Avatar answered Sep 01 '26 18:09

Armen Tsirunyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!