简体   繁体   English

找到指针的大小

[英]Finding the size of a pointer

printf("pointer: %d\n", sizeof(*void));

This line results in a syntax error because of the *. 由于*,此行导致语法错误。 What should I do to get it to work? 我该怎么办才能让它发挥作用?

You are currently trying to find out the size that is at address void. 您目前正在尝试找出地址无效的大小。 If you are looking to find the size of a void pointer perhaps try: sizeof(void*) instead. 如果您要查找void指针的大小,请尝试:sizeof(void *)。

printf("pointer: %zu\n", sizeof(void*));

should do what you want. 应该做你想做的事。 Use %zu and not %d as the pointer is an unsigned value and not a decimal. 使用%zu而不是%d,因为指针是无符号值而不是小数。

Edit: Something else that I just thought of for the first time, is %zu compiler dependent? 编辑:我第一次想到的其他东西是%zu编译器依赖吗? Do we need to do things differently on 32bit or 64bit architecture? 我们需要在32位或64位架构上做不同的事情吗?

printf("pointer: %d\n", sizeof(void*));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM