1
votes

Programming on C, weirdly I declare void edges(...) output as void and do the same thing below int main(void) however, I get an error message that I'm declaring a different output. I was wondering what is wrong with this?

#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

The error message on terminal which says conflicting types for the function edges???

1
void edges(int, int, RGBTRIPLE); is not void edges(int, int, (*RGBTRIPLE)[width]); Since you are using a VLA, void edges(int a, int b, RGBTRIPLE[a][b]); will work. With your current code shown, there is no need to link against -lcs50, -lcrypt or -lm -- add them when you use something that requires them. - David C. Rankin
Your function declaration takes two integers and one RGBTRIPLE, while your definition takes two integers and a 2D array of RGBTRIPLE values. These declarations specify different types and as such, are clearly conflicting. - nanofarad

1 Answers

2
votes

The short version of the story is -- your compiler is right...

If you look at your prototype:

void edges(int, int, RGBTRIPLE);

It takes three parameters, an int, int, and anonymous struct RGBTRIPLE. However, when you define the function, you define a 2D array of RGBTRIPLE, e.g.

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

See the difference? A single RGBTRIPLE in the prototype and RGBTRIPLE[height][width] in the definition.

The simplest way to fix your prototype is to provide named variables that can be used as the required dimensions for your 2D VLA, e.g.

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

Even better, do away with the prototype altogether and simply define edges above main() -- that way you eliminate the chance of any mismatch.