Can anyone help? I'm trying to complete CS50's filter-less but I keep getting the error. I feel like I am not understanding pointers properly. Could anyone explain or point me to a good resource?
helpers.c:17:8: error: incompatible pointer types assigning to 'int *' from 'BYTE *' (aka 'unsigned char *') [-Werror,-Wincompatible-pointer-types]
pb = &image[h][w].rgbtBlue;
^ ~~~~~~~~~~~~~~~~~~~~~
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
make: *** [<builtin>: helpers] Error 1
#include "helpers.h"
#include <stdlib.h>
#include <math.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
int h = height;
int w = width;
float rgbsum;
int *pb;
int *pg;
int *pr;
pb = &image[h][w].rgbtBlue;
pg = &image[h][w].rgbtGreen;
pr = &image[h][w].rgbtRed;
/*
Average Pixel Value:
Set RGB all to the same. {R, B , G} = rbgsum
Average the sum of all colors. (R G B) / 3.0
Return new values back to RBG.
*/
for (int i = 0; i < h; i )
{
for (int j = 0; j < w; j )
{
rgbsum = round((&b &g &r) / 3.0);
image[h][w].rgbtBlue = rgbsum;
image[h][w].rgbtGreen = rgbsum;
image[h][w].rgbtRed = rgbsum;
}
}
return;
}
CodePudding user response:
Without knowing more about RGBTriple it is hard to say for sure, but it seems to be a two-dimensional array of some struct containing RGB values. When you do
pb = &image[h][w].rgbtBlue;
You are dereferencing the "blue value" at that height and width. In this case, it looks like that value is a byte - you are dereferencing a byte. The issue is that pb
is an int pointer, meaning the types are incompatible, a byte pointer cannot be assigned to an int pointer. Your pointer types must match for this code to work, so change your pb
pointer type to match that of rgbtBlue.
CodePudding user response:
First of all it would be helpful to know how "RGBTRIPLE" is defined.
However from the error you got it seems that each colour is stored on an unsigned char.
If I'm correct there are two easy ways of solving it.
Option 1. - Change the values type to BYTE pointer or unsigned char pointer
BYTE *pb;
BYTE *pg;
BYTE *pr;
pb = &image[h][w].rgbtBlue;
pg = &image[h][w].rgbtGreen;
pr = &image[h][w].rgbtRed;
Option 2. - Don't use pointers at all and let the unsigned char be converted to int
int pb;
int pg;
int pr;
pb = image[h][w].rgbtBlue;
pg = image[h][w].rgbtGreen;
pr = image[h][w].rgbtRed;