首页 > 解决方案 > 我用相同的输出声明了相同的函数

问题描述

在 C 上编程,奇怪的是我将 void edges(...) 输出声明为 void 并在 int main(void) 下执行相同的操作,但是,我收到一条错误消息,说明我正在声明不同的输出。我想知道这有什么问题?

#include <math.h>
#include <stdio.h>
#include <cs50.h>

typedef struct{
    int  rgbtBlue;
    int  rgbtGreen;
    int  rgbtRed;
}
RGBTRIPLE;

int make_image_array(int);
void edges(int, int, RGBTRIPLE);

int main(void){
    edges(height, width, image[height][width]);
}



void edges(int height, int width, RGBTRIPLE image[height][width])
{}

1

终端上的错误消息显示功能边缘的类型冲突???

标签: cterminalcs50

解决方案


这个故事的简短版本是——你的编译器是对的......

如果您查看原型:

void edges(int, int, RGBTRIPLE);

它需要三个参数,一个int,,intanonymous struct RGBTRIPLE。但是,当您定义函数时,您定义了一个 2D 数组RGBTRIPLE,例如

void edges(int height, int width, RGBTRIPLE image[height][width])
{}

看到不同?RGBTRIPLE原型和RGBTRIPLE[height][width]定义中的单曲。

修复原型的最简单方法是提供可用作 2D VLA 所需尺寸的命名变量,例如

void edges(int a, int b, RGBTRIPLE[a][b]);

更好的是,完全取消原型并简单地edges在上面定义main()——这样你就消除了任何不匹配的机会。


推荐阅读