编译时出现奇怪的错误
I am getting a weird error when compiling
下面是我在课堂上布置家庭作业的代码段,但是当我尝试编译它时弹出:
[Error] invalid conversion from 'void*' to 'int*' [-fpermissive]
在指针 1-3 上,选择和两个选项。我仍然是一个新手程序员,我不确定为什么现在会发生这种情况。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *pointer1, *pointer2, *pointer3;
int *choice;
char * option;
char * option1;
pointer1 = malloc ( sizeof(int) );
pointer2 = malloc ( sizeof(int) );
pointer3 = malloc ( sizeof(int) );
choice = malloc ( sizeof(int) );
option = malloc ( sizeof(char) );
option1 = malloc ( sizeof(char) );
}
Malloc 函数将返回一个void*
。您需要将其转换为正确的类型,如下所示:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* pointer1, * pointer2, * pointer3;
int* choice;
char* option;
char* option1;
pointer1 = (int*) malloc(sizeof(int));
pointer2 = (int*) malloc(sizeof(int));
pointer3 = (int*) malloc(sizeof(int));
choice = (int*) malloc(sizeof(int));
option = (char*) malloc(sizeof(char));
option1 = (char*) malloc(sizeof(char));
// Clean up after yourself!!!
free(pointer1);
free(pointer2);
free(pointer3);
free(choice);
free(option);
free(option1);
return 0;
}
而且,不要忘记释放已用内存!
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- std::is_base_of表示ctor编译错误
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- Qt Cmake 错误编译"GuiSupportQt not found"
- Opengl 精度转换错误编译错误 E0415
- 库将ARM架构错误编译为架构X64
- RT 音频 Mac 错误 g++ 编译错误
- 错误编译Boost.log
- 错误编译QT创建者 / QT窗口小部件示例
- 错误编译MIPS32
- Visual Studio 2013 中的错误(编译和运行代码)
- 链接错误编译qt项目在visual 2010
- 无法用模板错误编译nsgmls
- 奇怪的错误.编译失败
- 如果有人调用c++中的方法,则强制错误(编译时)
- 来自autoconf测试的错误编译命令
- 时间限制超出错误C++编译
- SFML 2.3 和 CodeBlocks 错误编译