【问题标题】:incompatible types when returning type返回类型时不兼容的类型
【发布时间】:2019-05-20 19:10:53
【问题描述】:

convertToPoint 函数有问题。

int convertToPoint(int argc, char *argv[]) {
  struct point p;
  int x, y;

  p.x = atoi(argv[1]);
  p.y = atoi(argv[2]);

  return p;
}

期望返回一个point类型的结构,但收到以下错误:

错误:返回类型“struct point”但预期为“int”时类型不兼容 返回 p;

有什么问题?

【问题讨论】:

  • int convertToPoint(...) ==> struct point convertToPoint(...)
  • 您已明确表示该函数应返回int。当你返回其他东西时,为什么你期望它能够工作?
  • @Broman 你可以返回一个局部变量的实例(即它的值)。您不能返回的是指向本地的指针。这就是为什么你不能返回一个数组,因为它的名字被转换为一个指针,但是一个结构是可以的。
  • 我学到了一些新东西。谢谢。

标签: c ansi


【解决方案1】:

这是一个非常简单的问题。你说你想返回一个struct point,但你的代码说函数应该返回int

int convertToPoint(
^^^
ups, shall return int

所以只需将其更改为struct point - 比如:

#include <stdio.h>

struct point 
{
    int x;
    int y;
};

struct point convertToPoint(int argc, char *argv[]) {
    struct point p;
    p.x = atoi(argv[1]);
    p.y = atoi(argv[2]);
    return p;
}


int main(int argc, char *argv[]) {
    struct point p = convertToPoint(argc, argv);
    printf("%d %d\n", p.x, p.y);
}

也就是说 - 在不使用时传递 argc 有点奇怪。删除该函数参数或使用它来检查是否提供了足够的参数。喜欢:

    p.x = (argc > 1) ? atoi(argv[1]) : 0;
    p.y = (argc > 2) ? atoi(argv[2]) : 0;

另外请注意,我删除了 int x, y;,因为这些变量没有被使用。

【讨论】:

    【解决方案2】:

    问题是你告诉编译器你返回一个intint convertToPoint(...)。你想说struct point convertToPoint(...)

    如果您知道如何解析它,您看到的错误消息会告诉您这一点

    error: incompatible types when returning type ‘struct point’ but ‘int’ was expected return p;

    1. return p; -> 就编译器而言,这是个麻烦的语句。

    2. incompatible types when returning -> 你返回了错误的东西,检查你返回的是什么以及签名是什么

    3. type ‘struct point’ -> 这是你在体内返回的东西

    4. but ‘int’ was expected -> 这是来自您的函数签名的值。

    这是一个完整的例子

    // convert.c
    #include <stdio.h>
    #include <stdlib.h>
    
    struct point {
      int x;
      int y;
    };
    
    
    struct point convertToPoint(int argc, char *argv[]) {
      struct point p;
      int x, y;
    
      p.x = atoi(argv[1]);
      p.y = atoi(argv[2]);
    
      return p;
    }
    
    int main(int argc, char** argv) {
        struct point p = convertToPoint(argc, argv);
        printf("%d, %d", p.x, p.y);
    }
    

    证明它有效

    ~/src ❯❯❯ gcc -ansi convert.c -o convert                                                                                                                                               ✘ 139 
    ~/src ❯❯❯ ./convert 1 2
    1, 2%   
    

    最后,你可以做一些重构来清理它

    // convert.c
    #include <stdio.h>
    #include <stdlib.h>
    
    struct point {
      int x;
      int y;
    };
    
    
    struct point convertToPoint(char x[], char y[]) {
      struct point p;
    
      p.x = atoi(x);
      p.y = atoi(y);
    
      return p;
    }
    
    int main(int argc, char** argv) {
        //TODO: check for 2 args and print a helpful error message
        struct point p = convertToPoint(argv[0], argv[1]);
        printf("%d, %d", p.x, p.y);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-15
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 2011-07-13
      • 2014-08-29
      相关资源
      最近更新 更多