I'm a bit confused on whether to use *pointer or pointer -> for assigning a value withing a function to a pointer. For example, i have this program where i used * notation and got an error:
Then i changed it to this:
and now it works fine.
I was thinking it was because i'm changing something within a certain struct so i have to use -> instead of * when assigning the pointers inside the maxed_color function. Any help?
#include <stdio.h>
typedef struct
{
int red;
int green;
int blue;
} Color;
void maxed_color(Color c1, Color c2, Color *c3);
int main()
{
Color thirdcolor;
Color firstcolor = {20, 21, 25};
Color secondcolor = {19, 22, 24};
maxed_color(firstcolor, secondcolor, &thirdcolor);
printf("Max Color is: %d, %d, %d", thirdcolor.red, thirdcolor.green, thirdcolor.blue);
return 0;
}
void maxed_color(Color c1, Color c2, Color *c3)
{
if (c1.red > c2.red)
*c3 red = c1.red;
else
*c3 red = c2.red;
if (c1.green > c2.green)
*c3 green = c1.green;
else
*c3 green = c2.green;
if (c1.blue > c2.blue)
*c3 blue = c1.blue;
else
*c3 blue = c2.blue;
}
Then i changed it to this:
#include <stdio.h>
typedef struct
{
int red;
int green;
int blue;
} Color;
void maxed_color(Color c1, Color c2, Color *c3);
int main()
{
Color thirdcolor;
Color firstcolor = {20, 21, 25};
Color secondcolor = {19, 22, 24};
maxed_color(firstcolor, secondcolor, &thirdcolor);
printf("Max Color is: %d, %d, %d", thirdcolor.red, thirdcolor.green, thirdcolor.blue);
return 0;
}
void maxed_color(Color c1, Color c2, Color *c3)
{
if (c1.red > c2.red)
c3->red = c1.red;
else
c3->red = c2.red;
if (c1.green > c2.green)
c3->green = c1.green;
else
c3->green = c2.green;
if (c1.blue > c2.blue)
c3->blue = c1.blue;
else
c3->blue = c2.blue;
}
and now it works fine.
I was thinking it was because i'm changing something within a certain struct so i have to use -> instead of * when assigning the pointers inside the maxed_color function. Any help?