struct: Functions

Parameters and Return (1)

Returning entire structures

Constructor
struct point makepoint(int x, int y)
{
    struct point p;

    p.x = x;
    p.y = y;
    return p;
}

Parameters and Return (2)

Entire structure as parameter

struct point addpoints(struct point lhs, struct point rhs)
{
    lhs.x += rhs.x;
    lhs.y += rhs.y;
    return lhs;
}

Question: does the caller see the modification of lhs?

Parameters and Return (3)

Pointers to structures (“call by reference”)

void addtopoint(struct point *lhs, struct point rhs)
{
    (*lhs).x += rhs.x; /* precedence! */
    (*lhs).y += rhs.y;
}

Pointers to structures are very common ⟶ shortcut “->

void addtopoint(struct point *lhs, struct point rhs)
{
    lhs->x += rhs.x;
    lhs->y += rhs.y;
}