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. laravel路由导出和参数加密

    路由导出 代码位置:\vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteListCommand.php protected ...

  2. MySQL到底能否解决幻读问题

    先说结论,MySQL 存储引擎 InnoDB 在可重复读(RR)隔离级别下是解决了幻读问题的. 方法:是通过next-key lock在当前读事务开启时,1.给涉及到的行加写锁(行锁)防止写操作:2. ...

  3. NAT & 防火墙

    NAT 网络地址转换 NAT产生背景 今天,无数快乐的互联网用户在尽情享受Internet带来的乐趣.他们浏览新闻,搜索资料,下载软件,广交新朋,分享信息,甚至于足不出户获取一切日用所需.企业利用互联 ...

  4. Pytest使用pytest-html和allure生成测试报告

    Pytest-html 1.安装命令pip3 install pytest-html,如下图: 执⾏后,会在当前⽬录下⽣成 ⼀个report.html的⽂件,打开后会展示详细的测试报告,执行该命令py ...

  5. Part 11 to 20 Basic in C# continue

    Part 11-12 switch statement in C# switch statement break statement   if break statement is used insi ...

  6. linux 系统ssh超时设置

    1.修改client端的etc/ssh/ssh_config添加以下:(在没有权限改server配置的情形下) ServerAliveInterval 60 #client每隔60秒发送一次请求给se ...

  7. [bzoj5340]假面

    修改:维护g[i][j]表示第i个数为j的概率,从前往后转移 转移方程:g[id][i]=g[id][i-1]*p+g[id][i]*(1-p),初始g[i][a[i]]=1 询问:对于每一个人i,输 ...

  8. [luogu1737]旷野大计算

  9. 计算机网络 | 从 ChanelOption 到 Netty 底层

    概述 ChannelOption 是 Netty 中在构建引导类时可以填写的构建 Channel 的选项 其可以分为两部分,一部分为控制 Netty 自身底层运行的选项:另一部分则是操作系统创建 so ...

  10. vue-router的安装和使用

    第一种方式(简单):新建项目时用 vue-cli 手动选择 router 安装. 1.用键盘 ↓ 移动选择第三项" Manually select features"(手动配置): ...