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

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

  or similarly:

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:

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

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

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-27  12:30:53

How does “void *” differ in C and C++?的更多相关文章

  1. 工程优化方法中的“最速下降法”和“DFP拟牛顿法”的 C 语言实现

    这个小程序是研一上学期的“工程优化”课程的大作业.其实这题本可以用 MATLAB 实现,但是我为了锻炼自己薄弱的编码能力,改为用 C 语言实现.这样,就得自己实现矩阵的运算(加减乘除.求逆.拷贝):难 ...

  2. 【转载】#274 - Can't Overload if Methods Differ Only by ref and out Modifiers

    You can overload a method in a class, i.e. define two methods with the same name, if the methods hav ...

  3. differ比较两个字符串的差异

    "abcde","abdefk"  ---->-c,+f,+k "aba","aababb"    -----&g ...

  4. implicitly declaring function 'malloc' with type void *(unsigned long ) 错误 解决

    errror :   implicitly declaring function 'malloc' with type void *(unsigned long ) Be sure to includ ...

  5. 如何理解typedef void (*pfun)(void)

    问题: 在刚接触typedef void (*pfun)(void) 这个结构的时候,存在疑惑,为什么typedef后只有一"块"东西,而不是两"块"东西呢?那 ...

  6. C#中的null与void

    一.null: 1.明义,null是什么意思? null是指一个变量没有指向具体对象的有效引用. 这句话什么意思呢?意思就是 1).能够使用null修饰的是变量: 2).主要指的是引用. 那么这就引出 ...

  7. 你必须知道的指针基础-7.void指针与函数指针

    一.不能动的“地址”—void指针 1.1 void指针初探 void *表示一个“不知道类型”的指针,也就不知道从这个指针地址开始多少字节为一个数据.和用int表示指针异曲同工,只是更明确是“指针” ...

  8. js中 javascript:void(0) 用法详解

    点击链接不做任何事情: <a href="#" onclick="return false">test</a> <a href=& ...

  9. html 空链接 href="#"与href="javascript:void(0)"的区别

    #包含了一个位置信息 默认的锚是#top 也就是网页的上端 而javascript:void(0) 仅仅表示一个死链接 这就是为什么有的时候页面很长浏览链接明明是#但跳动到了页首 而javascrip ...

随机推荐

  1. swoole、swoft环境配置

    一.服务器环境 1.lnmp wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf ...

  2. 【java+selenium3】自动化截图 (十四)

    一.截图 1. Firefox浏览器截图 FirefoxDriver firefoxDriver = new FirefoxDriver(); firefoxDriver.getScreenshotA ...

  3. Cookie、Session、localStorage、sessionStorage区别和用法

    Cookie 在学习一个新知识点前,我们应该明白自己的学习目标,要带着疑问去学习,该小节须要了解 Cookies 什么是cookie,cookie的作用 cookie的工作机制,即cookie是运作流 ...

  4. c++学习笔记目录

    chapter name menu 一 从c到c++ 1.引用2.const关键词的用法3.动态内存分配4.内联函数5.函数重载6.函数的缺省参数7.结构化程序设计的不足8.面向对象的程序设计 二 类 ...

  5. 从源码分析 XtraBackup 的备份原理

    MySQL物理备份工具,常用的有两个:MySQL Enterprise Backup 和 XtraBackup. 前者常用于MySQL企业版,后者常用于MySQL社区版.Percona Server ...

  6. 5.0jemter(英文版)录制脚本,进行压力测试

    压力测试的目的:找到瓶颈.优化速率 1.jemter,Test Plan-->>Add-->>Threds(users)-->>Thred Group创建线程组 2 ...

  7. [bzoj1044]木棍分割

    第一个问题可以用贪心+二分解决第二个问题用f[i][j]表示i次分割后分割到j且满足条件的方案数,$f[i][j]=\sum_{k<j且sum[j]-sum[k]<=ans}f[i-1][ ...

  8. C++ and OO Num. Comp. Sci. Eng. - Part 2.

    本文参考自<C++ and Object-Oriented Numeric Computing for Scientists and Engineers>. 1. Basic Types ...

  9. GWAS分析结果中pvalue/p.ajust为0时如何处理?

    在GWAS分析的结果中,偶尔会遇到到pvalue为0的SNP位点,这时如果直接做曼哈顿或QQ图,会出错,因为log0无意义. 此时,该如何处理? 如果你用的是Plink1.9来做的GWAS,可加一个参 ...

  10. linux中的颜色

    echo -e "\033[30m 黑色字 \033[0m" echo -e "\033[31m 红色字 \033[0m" echo -e "\033 ...