Skip to content
Justice Tech School

Structures and Functions

Pass a pointer to a struct to avoid copying and to change it.

Example
#include <stdio.h>

typedef struct { int w, h; } Rect;
int area(const Rect *r) { return r->w * r->h; }
void grow(Rect *r) { r->w *= 2; }
int main(void) {
  Rect r = {3, 4};
  printf("%d ", area(&r));
  grow(&r);
  printf("%d\n", area(&r));
  return 0;
}

Result

12 24

Exercise

Pointer to struct.

int area(const Rect r)

Quiz Test

Why pass a pointer to a struct?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.