3

私はこれに出くわしました

#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; }

「c##_」という単語以外はすべて明らかですが、これはどういう意味ですか?

4

6 に答える 6

4

「くっつく」という意味なのでc_「くっついて」形にc_なる。この接着は、マクロで引数を置換した後に発生します。私の例を見てください:

#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;
}
于 2011-08-17T11:06:24.733 に答える
3

これはトークン貼り付け演算子と呼ばれます。例:

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

int main()
{
   paster(9);
}

出力

token9 = 9
于 2011-08-17T11:06:38.960 に答える
2

プリプロセッサの後、マクロは次のように展開されます。

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; }

##ディレクティブは、マクロパラメータとして渡すcの値を_に連結します。

于 2011-08-17T11:08:58.600 に答える
2

として渡された名前にアンダースコアを追加する連結ですc。だから使うときは

DsHook(a,b,Something)

その部分はに変わります

if (!Something_) 
于 2011-08-17T11:06:44.343 に答える
1

単純なもの:

#define Check(a) if(c##x == 0) { }

コールサイト:

int varx; // Note the x
Check(var);

次のように拡張されます:

if(varx == 0) { }
于 2011-08-17T11:08:44.753 に答える
1

これはトークン連結と呼ばれ、前処理中にトークンを連結するために使用されます。たとえば、次のコードは、c、c_、c_spamの値の値を出力します。

#include<stdio.h>

#define DsHook(a,b,c) if (!c##_) \
    {printf("c=%d c_ = %d and c_spam = %d\n",\
    c, c##_,c##_spam);}

int main(){
    int a,b,c=3;
    int c_ = 0, c_spam = 4;

    DsHook(a,b,c);

    return 0;
}

出力:

c=3 c_ = 0 and c_spam = 4
于 2011-08-17T11:19:53.787 に答える