我想为c_ sizeof()操作符实现另一个自定义函数。我从互联网上得到了很低的定义,而且它的工作非常好。
#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)但是,与预处理器(#Define宏)函数不同,我想将其实现为一个单独的函数,如下所示。有人能帮我完成sizeof_custom()?的定义吗?
备注:我为什么要以这种方式实现,是为了了解我在尝试用空指针实现它时遇到困难的原因,并找到差异(我知道指针不允许算术操作,但是如何构建这个函数?)请帮帮忙。
#include <stdio.h>
return_type sizeof_custom ( arg )
{
....
....
// return the size of the variable
}
void main()
{
int a; // It can be int, float, char,....
printf("size = %d", sizeof_custom(a));
}发布于 2021-08-01 17:05:27
不可能使用其他语言特性实现sizeof操作符。它是语言的一部分,必须由编译器实现。最重要的是:
不衰减到pointers
sizeof中不是的。
https://stackoverflow.com/questions/68612626
复制相似问题