首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

小弟求前辈帮看看上面代码错哪了

2013-01-23 
小弟求前辈帮看看下面代码哪里错了#include iostream#include stdio.h#include malloc.husing names

小弟求前辈帮看看下面代码哪里错了
#include <iostream>
#include <stdio.h>
#include <malloc.h>

using namespace std;

int main()
{
    int *a_ptr = (int *)malloc(int);
    *a_ptr = 5;
    printf("%d", *a_ptr);
    return 0;
}
编译出现9: error: expected primary-expression before 'int'
错误,小弟初学c语言;求前辈指教。谢谢。
[解决办法]
int *a_ptr = (int *)malloc(sizeof(int)); //int *a_ptr = (int *)malloc(int);
[解决办法]
malloc(len),这里的len应该是数值,你却用int,让编译程序不知道到底要多少

看你的意思,应该是:
malloc(sizeof(int))

C++中有更亲切的做法:
int *a_ptr = new int();

最后,提醒一下,申请的内存一定要记得释放。
new的,用delete
malloc的,用free

热点排行