简体   繁体   English

为什么我从“不兼容类型'void *'”错误中“分配'int *'?

[英]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结尾。

The type returned from malloc is always void*, which can be cast to the desired type of data. 从malloc返回的类型总是void *,可以转换为所需的数据类型。

You will need to do: (int*) malloc(nombre_lettres * sizeof(int)); 你需要做:(int *)malloc(nombre_lettres * sizeof(int));

source: cplusplus 来源: cplusplus

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:语义问题从不兼容类型“ void”分配给“ int” - Error: Semantic issue Assigning to 'int' from incompatible type 'void' 从不兼容的void *中分配int * - Assigning int* from incompatible void * 错误:从“void *”类型分配类型“值”时出现不兼容的类型? - Error: incompatible types when assigning to type 'values' from type 'void *'? 分配给'uint16_t'类型错误时,为什么会得到不兼容的类型? - Why do I get an incompatible types when assigning to type 'uint16_t ' error? 为什么会出现“分配时不兼容的类型”错误? - Why do I get “Incompatible types when assigning” error? 从可可项目中的不兼容类型“ void *”错误分配给“ MyPrivateData *” - Assigning to “MyPrivateData *” from incompatible type 'void *' error in cocoa project 为什么会出现错误:“int”类型的参数与 C 中“char*”类型的参数不兼容? - Why do I get the error: argument of type "int" is incompatible with parameter of type "char*" in C? 从“int (int *)”分配给“void (*)(void *)”的不兼容指针类型 - incompatible pointer types assigning to 'void (*)(void *)' from 'int (int *)' 从不兼容的类型'int'分配[custom typdef] - Assigning to [custom typdef] from incompatible type 'int' 将指针分配给多维数组时,为什么会出现“不兼容的指针类型” - Why do I get “incompatible pointer type” when assigning pointers to a multidimensional array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM