我在codeacademy上学习c,并且从不兼容的类型“ void *”错误中分配了“ int *”。我的文件是一个.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));
非常感谢您的帮助,对于我在英语上的任何错误,我们深表歉意。
您的文件名称似乎是TP.C
。
在Unix传统中,C源文件被命名为*.c
(小写的c),而C ++源文件被命名为*.C
(大写的C)。由于*.cpp
Windows不区分大小写,因此在Windows上使用了替代方法,但是MacOS上的gcc足够像unix一样,它将*.C
文件视为C ++。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句