【发布时间】:2017-12-01 07:29:03
【问题描述】:
所以我有这段代码可以正常工作
#include <stdio.h>
#define B(X) &X
struct abc{
char temp[2];
};
void print(char *string)
{
printf("%s\n", string);
}
int main()
{
struct abc *pAbc = malloc(sizeof(struct abc));
pAbc->temp[0] = 'a';
pAbc->temp[1] = '\0';
print(pAbc->temp);
free(pAbc);
return 0;
}
但这不起作用
#include <stdio.h>
#define B(X) &X
struct abc{
char temp[2];
};
void print(char **string)
{
printf("%s\n", *string);
}
int main()
{
struct abc *pAbc = malloc(sizeof(struct abc));
pAbc->temp[0] = 'a';
pAbc->temp[1] = '\0';
print(B(pAbc->temp));
free(pAbc);
return 0;
}
据我了解,宏定义应该返回变量的地址。
这个函数与 int 配合得很好。
编辑:
对不起,我忘了提到我想做的事情,我想传递一个指向函数指针的指针,因为我只想为一些编译路径这样做,我想设置一个选项,比如
if #defined WIN32
print(B(varible))
elif #defined GCC
print(varible)
【问题讨论】:
-
@RingØ 我不确定这是不是骗子,你为什么这么说?
-
这是一个可能的骗局,因为问题的基本原因是
x和&x返回相同的地址。阅读提到的问答将避免错误。如果您不同意,请不要投票。 -
pAbc->temp和&pAbc->temp不是一回事,是否通过宏完成无关紧要。