用C冲突的类型错误 [英] conflicting types error in C

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

问题描述

有关下列C code(对于交换两个数字)我正在为交换功能冲突的类型错误。

For the following C code (for swapping two numbers) I am getting the "conflicting types" error for swap function.

#include <stdio.h>
#include <stdlib.h>
void main()
{
     int a,b;
     printf("enter the numbers to be swapped");
     scanf("%d%d",&a,&b);
     printf("before swap");
     printf("a=%d,b=%d",a,b);
     swap(&a,&b,sizeof(int));
     printf("after swap");
     printf("a=%d,b=%d",a,b);
  getch();   
}
void swap(void *p1,void *p2,int size)
{
     char buffer[size];
     memcpy(buffer,p1,size);
     memcpy(p1,p2,size);
     memcpy(p2,buffer,size);
     return(0);
}

有谁能够电话为什么错误来了?结果
该解决方案是什么?。

can anybody tel why that error is coming?
what is the solution for that?

推荐答案

的问题是在使用前交换并没有声明的。因此,它被分配一个默认的签名之一,这将这种情况下不匹配的实际签名。 牛逼:

The problem is that swap was not declared before it is used. Thus it is assigned a "default signature", one which will in this case not match its actual signature. Quote Andrey T:

的参数通过一组传递
  严格定义的转换。 为int *
  指针会 INT传递*
  指针,例如。换一种说法,
  参数类型是暂时
  从参数类型演绎。只要
  返回类型被假定为 INT

除此之外,您的code产生很多其他的警告。如果使用 GCC ,编译 -Wall -pedantic (或甚至 -Wextra ),并确保继续编写额外的功能之前解决每个警告。此外,您可能要告诉编译器无论你是写ANSI C( -ansi )或C99( -std = C99 )。

Aside from that, your code produces a bunch of other warnings. If using gcc, compile with -Wall -pedantic (or even with -Wextra), and be sure to fix each warning before continuing to program additional functionality. Also, you may want to tell the compiler whether you are writing ANSI C (-ansi) or C99 (-std=c99).

一些言论:


    逗号后的
  • 把空间。

  • 返回 INT

    • 并使其返回0 返回EXIT_SUCCESS

    • Put spaces after commas.
    • Make main return an int.
      • And make it return 0 or return EXIT_SUCCESS.

      您可能需要使用的malloc 分配可变大小的缓冲区。这也将与老的编译器工作:

      You may want to use malloc to allocate a buffer of variable size. That will also work with older compilers:

      void swap(void *p1, void *p2, int size) {
          void *buffer = malloc(size);
          memcpy(buffer, p1, size);
          memcpy(p1, p2, size);
          memcpy(p2, buffer, size);
          free(buffer);
      }
      


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

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