ç冲突的类型错误 [英] C conflicting type bug

查看:165
本文介绍了ç冲突的类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译此我被GCC,我有冲突的类型 pack_cui(C)告诉

我不明白我怎么可以让冲突的类型,因为我知道我传递一个的char * 。我是新的C,所以我敢肯定,我失去了一些东西明显。

  INT的main(){
  字符* C =的malloc(sizeof的(字符)* 4);
  C [0] = 1;
  C [1] = 2;
  C [2] = 1;
  C [3] = 4;
  pack_cui(℃);
  返回0;
}unsigned int类型pack_cui(字符* C){
  unsigned int类型new_int = 0;
  无符号整型我;
  对于(I = 0; I&下; 4;我++){
    new_int = new_int | (无符号整数)(符号int)C [I]
    如果(ⅰ= 3!)new_int = new_int&所述;&下; 8;
  }
  返回new_int;
}

我收到的错误是

  hw12.c:17:14:错误:冲突的类型'pack_cui
 unsigned int类型pack_cui(字符* C){
              ^
hw12.c:13:5:注意:previous的pack_cui'隐式声明在这里
     pack_cui(℃);


解决方案

当你调用一个未声明的功能,C89标准要求隐式声明。该声明将是:

  INT pack_cui();

()是不一样的(无效),该函数接受一个,表示未指定的若干参数,而(无效)表示为零。这是从pre-ISO / ANSI C遗留下来的,早在K&放大器; R天。

您不希望出现这种情况,因为这是错误的声明。在上面创建自己的声明,上面的main()

 无符号pack_cui(字符*);

When I try to compile this I'm being told by gcc that I have conflicting types for pack_cui(C).

I don't see how I could be getting conflicting types as I know I'm passing in a char*. I'm new to C so I'm sure I'm missing something obvious.

int main(){
  char* C = malloc(sizeof(char) * 4);
  C[0] = 1;
  C[1] = 2;
  C[2] = -1;
  C[3] = 4;
  pack_cui(C);
  return 0;
}

unsigned int pack_cui(char* C){
  unsigned int new_int = 0;
  unsigned int i;
  for(i = 0; i < 4; i++){
    new_int = new_int | (unsigned int)(signed int)C[i];
    if(i != 3) new_int = new_int << 8;
  }
  return new_int;
}

The error I received was

hw12.c:17:14: error: conflicting types for ‘pack_cui’
 unsigned int pack_cui(char* C){
              ^
hw12.c:13:5: note: previous implicit declaration of ‘pack_cui’ was here
     pack_cui(C);

解决方案

When you call an undeclared function, the C89 standard mandates an implicit declaration. This declaration would be:

int pack_cui();

The () is not the same as (void), it indicates that the function takes an unspecified number of arguments, whereas (void) means zero. This is left over from pre-ISO/ANSI C, back in the K&R days.

You don't want that, because that is the wrong declaration. Create your own declaration at the top, above main():

unsigned pack_cui(char *);

这篇关于ç冲突的类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆