【问题标题】:Type mismatch in CC中的类型不匹配
【发布时间】:2014-12-19 08:28:30
【问题描述】:

我写了这段代码,但它说显示板的声明存在类型冲突。我觉得很好。有人可以帮忙吗?

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int board[9][9] = {
            {0,0,5,9,0,2,3,8,7},
            {9,4,0,1,0,7,6,0,2},
            {2,8,7,5,3,0,4,0,0},
            {0,5,2,3,6,0,0,0,1},
            {4,0,9,0,5,1,2,6,8},
            {1,0,6,0,2,0,0,3,4},
            {5,0,8,4,0,0,1,9,6},
            {3,9,0,0,1,8,7,2,0},
            {0,6,0,2,9,5,8,0,3}
    };
    char* board_output = (display_board(board));
    printf ("%s", &board_output);
    return 0;
    }

    char* display_board (int board_input[9][9])
    {
        int i;
        int j;
        char* output = "";
        for (i=0; i<9; i++) {
            for (j=0; j<9; j++) {
                    output = strcat(strcat(output, board_input[i][j]),  ", ");
            }
            output = strcat(output, ", ");
        }

        return output;
    }

谢谢

卡比尔

【问题讨论】:

  • 另外,你内心的strcat是错误的。
  • 你需要为函数添加一个原型:I.E. char* display_board (int board_input[9][9]);这个原型需要在任何其他函数之外并且在 main() 之前
  • 当没有使用原型时,C默认所有项目都是int,包括返回值。这就是编译器产生错误消息的原因

标签: c function types


【解决方案1】:

在调用该函数之前需要一个函数声明:

char* display_board (int board_input[9][9]) ;  

您还尝试在此处写入字符串文字output

strcat(strcat(output, board_input[i][j]),  ", "); //this syntax is not readable
                                                  //split the calls into
                                                  //separate lines

而是为它保留一些内存

char* output = calloc( 512 , sizeof( char ) ) ;

strcat() 需要一个字符串而不是整数,所以这是不正确的:strcat(output, board_input[i][j])


可能还有更多错误,请先修复这些错误,然后重试。

【讨论】:

  • 在这种情况下,将 main() 移动到最后也可以。
  • 如果将output声明为数组,则必须是静态的!
  • @IngoLeonhardt 已修复。我没有解析那么远的代码。
【解决方案2】:

main()调用之前需要函数原型

添加这个

char* display_board (int board_input[9][9]);

就在标题之后

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2015-05-28
    • 2020-03-31
    相关资源
    最近更新 更多