【发布时间】:2019-12-20 22:47:03
【问题描述】:
我在 codeacademy 上学习 c,我得到一个 assigning to 'int *' from in compatible type 'void *' 错误。我的文件是一个 .c 文件,其中包括 stdlib.h。我不明白错误,似乎更正使用了相同的代码行。
我正在尝试使用 malloc 创建一个数组。
我试图找到其他主题的答案。似乎 malloc 不是最好的方法,但我想找到一种方法让它工作。
我在mac上,我用emacs编码,gcc编译。
这是我的代码的一部分:
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);
}
这是解决方案: (我试图翻译一些变量)
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);
错误是:
TP.C:17:16: error: assigning to 'int *' from incompatible type 'void *'
decouverte = malloc(nombre_lettres * sizeof(int));
非常感谢您的帮助,如果我在英语中有任何错误,我很抱歉。
【问题讨论】:
-
也许你是用 C++ 编译器而不是 C 编译器来编译它。
-
非常感谢您的快速答复。我这样编译我的文件:gcc -o tp tp.c