小弟求前辈帮看看上面代码错哪了
小弟求前辈帮看看下面代码哪里错了
#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
#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