且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C中实现sizeof运算符

更新时间:2023-11-10 09:01:52


谁能告诉我如何实现C语言中的sizeof
Can anyone tell me how to implement sizeof in C language



已经有了它的实现。您想使用它还是创建自己的?

There already is an implemenation of it. Do you want to use it or to create your own?


yes ..我想编写一个提供相同功能的函数...
yes.. I want to write a function which provide the same functionality...


由于你不能写一个以类型作为参数的函数,你将无法做你想做的事。


你可以以编程方式获取类型的大小但是代码必须特定于类型。


sizeof运算符存在,因为没有真正的其他方式来获得它提供的功能,它是有用的。
Since you can not write a function that takes a type as an argument you will not be able to do what you wish.

You can get the size of a type programatically but the code would have to be specific to the type.

The sizeof operator exists because there is no real other way to get the functionality it provides and it is something that is useful.