首页 > 编程语言 > 详细

How does “void *” differ in C and C++?

时间:2015-09-22 18:36:38      阅读:222      评论:0      收藏:0      [点我收藏+]

http://www.geeksforgeeks.org/g-fact-12-2/

C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not; this idiomappears often in C code using malloc memory allocation. For example, the following is valid in C but not C++:

 1 void* ptr; 2 int *i = ptr; /* Implicit conversion from void* to int* */ 

or similarly:

 1 int *j = malloc(sizeof(int) * 5); /* Implicit conversion from void* to int* */ 

In order to make the code compile in both C and C++, one must use an explicit cast:

1 void* ptr;
2 int *i = (int *) ptr;
3 int *j = (int *) malloc(sizeof(int) * 5);

Source:
http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

How does “void *” differ in C and C++?

原文:http://www.cnblogs.com/lyleslie/p/4829374.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!