#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct ident_
{
  char *no_plate;
  int colour;
} ident;

typedef struct
car_
{
  int speed;
  int gear;
  char *name;
  ident identification; 
} car;


int free_vars (car *c)
{
  if (c == NULL ||
      c->name == NULL ||
      c->identification.no_plate == NULL)
    {
      printf("BAD DATA\n");
      return 1;
    }
  
  free(c->identification.no_plate);
  
   
  free(c->name);

  
    
  free(c);
}

int main () {
  //NULL test 
  free_vars(NULL);
  
  car *ford;
  ford = malloc(sizeof(car));
  printf("Size of 'car': %lu\n", sizeof(car));
  
  char car_name[] = "Escort";
  printf("Size of 'car_name': %lu\n", strlen(car_name));

  ford->name = calloc(strlen(car_name) + 1, sizeof(char));
  ford->identification.no_plate == NULL;
  if (ford->name == NULL)
    {
      printf("NULL\n");
      return 1;
    }
  strcpy(ford->name, car_name);
  printf("Size of 'car_name' pointed to by pointer: %lu\n", strlen(ford->name));
  printf("Size of 'pointer': %lu\n", sizeof(ford->name));
  printf("Name of car: %s\n", ford->name);
  

  //time to free both the string and the struct pointer
  free_vars(ford);



  car *skoda;
  skoda = malloc(sizeof(car));
  char *skoda_name = "Octavia";
  
  skoda->name = NULL;
  skoda->speed = 1;
  

  skoda->identification.no_plate = malloc(strlen(skoda_name) + 1);
  
  printf("size of ident->no_plate pointer : %lu\n",  sizeof(skoda->identification.no_plate));
  
  strcpy(skoda->identification.no_plate, skoda_name);
  printf("skoda name: %s\n", skoda->identification.no_plate);

  printf("colour: %d\n", skoda->speed);
  

  free_vars(skoda);



  return 0;
}
  


