Types and Structs


Collapse Content

C doesn't have classes, but it allows user-defined types and structs.

// Typedefs can be used to create type aliases
typedef int my_type;
my_type my_type_var = 0;

// Structs are just collections of data, the members are allocated sequentially,
// in the order they are written:
struct rectangle {
  int width;
  int height;
};

It's not generally true that sizeof(struct rectangle) == sizeof(int) + sizeof(int) due to potential padding between the structure members (this is for alignment reasons). See StackOverflow

void function_1()
{
  struct rectangle my_rec;

  // Access struct members with .
  my_rec.width = 10;
  my_rec.height = 20;

  // You can declare pointers to structs
  struct rectangle *my_rec_ptr = &my_rec;

  // Use dereferencing to set struct pointer members...
  (*my_rec_ptr).width = 30;

  // ... or even better: prefer the -> shorthand for the sake of readability
  my_rec_ptr->height = 10; // Same as (*my_rec_ptr).height = 10;
}

// You can apply a typedef to a struct for convenience
typedef struct rectangle rect;

int area(rect r)
{
  return r.width * r.height;
}

if you have large structs, you can pass them "by pointer" to avoid copying the whole struct:

int areaptr(const rect *r)
{
  return r->width * r->height;
}
Contact Us
Sign in or email us at [email protected]