[英]Why do I get “assigning to 'int *' from incompatible type 'void *' ” error?
I'm learning c on codeacademy and I get an assigning to 'int *' from incompatible type 'void *' error. 我在codeacademy上学习c,我从不兼容的类型'void *'错误中分配给'int *' 。 My file is a .c file, which includes stdlib.h.
我的文件是.c文件,其中包含stdlib.h。 I don't understand the error, it seems that the corrections uses the same lines of code.
我不明白错误,似乎更正使用相同的代码行。
I'm trying to create an array using malloc. 我正在尝试使用malloc创建一个数组。
I tried to find the answer on other topics. 我试图找到其他主题的答案。 It seems that malloc is not the best way to do it but i would like to find a way to make it work.
似乎malloc不是最好的方法,但我想找到一种方法使它工作。
I'm on a mac, I use emacs to code et gcc to compile. 我在Mac上,我使用emacs编码和gcc进行编译。
Here's a part of my code : 这是我的代码的一部分:
int main(int argc, char *argv[])
{
char mot_secret[] = "PISCINE";
int nombre_lettres = strlen(mot_secret);
int *pnombre_lettres = &nombre_lettres;
int *decouverte = NULL;
int compteur = 10;
decouverte = malloc(nombre_lettres * sizeof(int));
if (decouverte == NULL)
{
exit(0);
}
And here's the solution : ( I tried to translate some of the variables) 这是解决方案:(我试图翻译一些变量)
int main(int argc, char* argv[])
{
char lettre = 0;
char secretword[100] = {0};
int *lettreTrouvee = NULL;
long coupsRestants = 10; // Compteur de coups restants (0 = mort)
long i = 0;
long wordSize = 0;
wordSize = strlen(secretWord);
lettreTrouvee = malloc(tailleMot * sizeof(int)); // creates a dynamic array of the size of the original)
if (lettreTrouvee == NULL)
exit(0);
The error is : 错误是:
TP.C:17:16: error: assigning to 'int *' from incompatible type 'void *'
decouverte = malloc(nombre_lettres * sizeof(int));
Thank you very much for your help and I'm sorry if I make any mistakes in English. 非常感谢你的帮助,如果我在英语中犯了任何错误,我很抱歉。
It appears that your file's name is TP.C
. 您的文件名称似乎是
TP.C
In the unix tradition, C source files are named *.c
(lowercase c), and C++ source files are named *.C
(capital C). 在unix传统中,C源文件名为
*.c
(小写c),C ++源文件名为*.C
(大写C)。 The alternative *.cpp
is used on Windows because of its case-ignorant filesystem, but gcc on MacOS is sufficiently unix-like that it is treating your *.C
file as C++. 替代
*.cpp
在Windows上使用是因为它不区分大小写的文件系统,但MacOS上的gcc与unix类似,它将*.C
文件视为C ++。
Given this error message: 鉴于此错误消息:
TP.C:17:16: error: assigning to 'int *' from incompatible type 'void *' decouverte = malloc(nombre_lettres * sizeof(int));
it appears your file is named TP.C
. 看来你的文件名为
TP.C
The upper-case .C
file extension causes GCC to compile the file as C++ : 大写
.C
文件扩展名导致GCC将文件编译为C ++ :
file.cc file.cp file.cxx file.cpp file.CPP file.c++ file.C
C++ source code that must be preprocessed.
必须预处理的C ++源代码。 Note that in '.cxx', the last two > must both be literally 'x'.
请注意,在'.cxx'中,最后两个>必须都是字面上的'x'。 Likewise, '.C' refers to a literal capital C.
同样,'。C'指的是文字资本C.
You need to use lower-case. 你需要使用小写。 Rename your file so that it ends with a
.c
- lower-case c
. 重命名您的文件,使其以
.c
- 小写c
结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.