Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

What is the difference between using * and -> for assigning pointe

$
0
0
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:

#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?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>