#include <stdio.h>


struct expensive; // Forward declaration, ignore
// One could also use a struct expensive * (a pointer) instead
// of this structure. IMO giving it a name is the better option.
struct expensive_handle {
  struct expensive * target;
};

// Store the simple data members as usual, store a pointer to a
// handle (pointer) to the expensive ones
struct my_struct {
  int simple;
  struct expensive_handle * handle;
};

struct expensive {
  int content; // whatever
};


struct my_struct * new() {
  struct my_struct * data = malloc(sizeof(*data));
  // Error handling please
  // Set simple data members
  data->handle = malloc(sizeof(*(data->handle)));
  // Error handling please
  data->handle->target = NULL;
  return data;
}

int get_expensive(struct my_struct const * ptr) {
  if (ptr->handle->target == NULL) {
    ptr->handle->target = malloc(sizeof(struct expensive));
    // Error handling please
    puts("A hell of a computation just happened!");
    ptr->handle->target->content = 42; // WOO
  }
  return ptr->handle->target->content;
}

int main(void) {
  struct my_struct * p = new();
  printf("%d\n", get_expensive(p));
  printf("%d\n", get_expensive(p));
}
