我试图实现这个问题中给出的内容。实施规模
#include <stdio.h>
#include <stdint.h>
#define my_sizeof(type) ((char*)(&type + 1)-(char*)(&type))
int main()
{
printf("Size of int %d \n",my_sizeof(int));
return 0;
}
但是,当我编译时,出现以下错误。
test.c:10:44: error: expected expression before ‘int’
printf("Size of int %d \n",my_sizeof(int));
^
test.c:5:35: note: in definition of macro ‘my_sizeof’
#define my_sizeof(type) ((char*)(&type + 1)-(char*)(&type))
^
test.c:10:44: error: expected expression before ‘int’
printf("Size of int %d \n",my_sizeof(int));
^
test.c:5:54: note: in definition of macro ‘my_sizeof’
#define my_sizeof(type) ((char*)(&type + 1)-(char*)(&type))
^
((char*)(&int + 1)-(char*)(&int))
您的宏正在尝试采用一种类型的地址。您可以通过在宏中包含一个带有局部变量的整个块来使宏更长(但是宏将无法按您想要的方式工作),或者仅对变量而非类型使用宏。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句