(void *)0:这是什么演员?

编程入门 行业动态 更新时间:2024-10-27 10:20:04
本文介绍了(void *)0:这是什么演员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好! 以下示例代码来自string.h

展开 | 选择 | 换行 | 行号

解决方案

在C中使用NULL作为值为0的指针。即,空指针。 void *是指向任何类型的指针,因此(void *)0是整数值0到任何类型的指针的转换。 在C ++中不使用NULL。 / blockquote>

除了你注意到的这个例子之外,void *类型在C和C ++中有许多合法用途。 void * type声明一个通用指针变量。由于编译器不知道你打算用这样的变量指出什么,它不会让你取消引用指针。但是,您可以将这样的指针传递给函数。 C程序可以使用void *指针来近似多态和信息隐藏。 C ++可能有更好的方法来实现这些目标。

谢谢你们的答案。

虚空*除了你注意到的这个例子之外,*类型在C和C ++中有许多合法用途。

你能告诉我使用这个的例子或者给我参考自己学习吗?我对如何使用这样的类型作为参数传递感兴趣。在函数中传递这些参数的用法是什么? 请注意我不使用C ++,所以关于C的例子是我的主要兴趣。 再次感谢。

Hello! The below sample code is from string.h

Expand|Select|Wrap|Line Numbers

解决方案

NULL is used in C as a pointer with a value of 0. That is, a null pointer. void* is a pointer to any type, so (void*)0 is a cast of the integer value 0 to a pointer to any type. NULL is not used in C++.

The void* type has many legitimate uses in C and C++ beyond this one example that you noted. The void* type declares a generic pointer variable. Since the compiler doesn''t know what you intend to point at with such a variable, it won''t let you dereference the pointer. However, you can pass such a pointer to and from functions. A C program can use void* pointers to approximate polymorphism and information hiding. C++ probably has better ways to achieve these goals.

Thank you both for your answers.

The void* type has many legitimate uses in C and C++ beyond this one example that you noted.

Can you please give me examples of using this or give me references to study this by myself? I am interested on how a type like this can be used as parameter passing. And what is the usage of passing such parameters in functions? Please note that I don''t use C++, so examples on C is my main interest. Thanks again.

更多推荐

(void *)0:这是什么演员?

本文发布于:2023-11-25 22:22:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:演员   这是什么   void

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!