Struct
  • Define user-defined type with struct
  • Class in C++ is an extension of struct
  • Define a Type with Struct
    // Car.h
    #ifndef CAR_H
    #define CAR_H
    
    struct Car{
    	int year;
    	int cylinder;
    };
    
    #endif
    		
    // main.c
    #include <stdio.h>
    #include "Car.h"
    
    int main()
    {
    	struct Car c;
    
    	c.year = 2016;
    	c.cylinder = 4;
    
    	printf("Year: %d\n", c.year);
    	printf("Cylinder: %d\n", c.cylinder);
    
    	return 0;
    }
    		
    Define a Type with Struct and Typedef
    // Car.h
    #ifndef CAR_H
    #define CAR_H
    
    typedef struct
    {
    	int year;
    	int cylinder;
    } Car;
    
    #endif
    		
    // main.c
    #include <stdio.h>
    #include "Car.h"
    
    int main()
    {
    	Car c;
    
    	c.year = 2016;
    	c.cylinder = 4;
    
    	printf("Year: %d\n", c.year);
    	printf("Cylinder: %d\n", c.cylinder);
    
    	return 0;
    }
    		
    Copy a Struct

  • Copying a struct means copying every element from one struct to another
  • // Car.h
    #ifndef CAR_H
    #define CAR_H
    
    typedef struct
    {
    	int year;
    	int cylinder;
    } Car;
    
    #endif
    		
    // main.c
    #include <stdio.h>
    #include "Car.h"
    
    int main()
    {
    	Car c;
    	c.year = 2016;
    	c.cylinder = 4;
    
    	Car temp;
    	temp = c;
    
    	printf("Year: %d\n", temp.year);
    	printf("Cylinder: %d\n", temp.cylinder);
    
    	return 0;
    }
    		
    Pass Struct by Value to Function
  • Pass a struct to a function by value, make a copy in the function
  • Retun a struct, return a copy
  • // Car.h
    #ifndef CAR_H
    #define CAR_H
    
    typedef struct {
    	int year;
    	int cylinder;
    } Car;
    
    #endif
    		
    // main.c
    #include <stdio.h>
    #include "Car.h"
    
    Car getCar()
    {
    	Car car;
    	car.year = 2016;
    	car.cylinder = 4;
    
    	return car;
    }
    
    void display(Car car)
    {
    	printf("Year: %d\n", car.year);
    	printf("Cylinder: %d\n", car.cylinder);
    }
    
    int main()
    {
    	Car c;
    
    	c = getCar();
    
    	display(c);
    
    	return 0;
    }
    		
    Pass Struct by Reference to Function
    // Car.h
    #ifndef CAR_H
    #define CAR_H
    
    typedef struct
    {
    	int year;
    	int cylinder;
    } Car;
    
    #endif
    		
    #include <stdio.h>
    #include "Car.h"
    
    void getCar(Car *car)
    {
    	//(*car).year = 2016;
    	//(*car).cylinder = 4;
    	car->year = 2016;
    	car->cylinder = 4;
    }
    
    void display(Car *car)
    {
    	printf("Year: %d\n", car->year);
    	printf("Cylinder: %d\n", car->cylinder);
    }
    
    int main()
    {
    	Car c;
    
    	getCar(&c);
    
    	display(&c);
    
    	return 0;
    }
    		
    Use Struct in Array
    // Car.h
    #ifndef CAR_H
    #define CAR_H
    
    typedef struct
    {
    	int year;
    	int cylinder;
    } Car;
    
    #endif
    		
    #include <stdio.h>
    #include "Car.h"
    
    void setCars(Car cars[], int size)
    {
    	cars[0].year = 2016;
    	cars[0].cylinder = 4;
    
    	cars[1].year = 2005;
    	cars[1].cylinder = 6;
    
    	cars[2].year = 1999;
    	cars[2].cylinder = 6;
    }
    
    void sort(Car cars[], int size)
    {
       for (int i = 0; i < size-1; i++)
           for (int j = 0; j < size-i-1; j++)
               if (cars[j].year > cars[j+1].year)
    	   {
    		   Car temp = cars[j];
    		   cars[j] = cars[j+1];
    		   cars[j+1] = temp;
    	   }
    }
    
    void display(Car cars[], int size)
    {
    	for(int i = 0; i < size; i++)
    	{
    		printf("Year: %d\n", cars[i].year);
    		printf("Cylinder: %d\n\n", cars[i].cylinder);
    	}
    }
    
    int main()
    {
    	Car cars[3];
    
    	setCars(cars, 3);
    
    	display(cars, 3);
    
    	sort(cars, 3);
    
    	display(cars, 3);
    
    	return 0;
    }
    		
    Use Struct in Pointer Array

    #ifndef CAR_H
    #define CAR_H
    
    typedef struct
    {
    	int year;
    	int cylinder;
    } Car;
    
    #endif
    		
    #include <stdio.h>
    #include <stdlib.h>
    #include "Car.h"
    
    Car *getCars()
    {
    	Car *cars = (Car *) malloc(3*sizeof(Car));
    
    	cars[0].year = 2016;
    	cars[0].cylinder = 4;
    
    	cars[1].year = 2005;
    	cars[1].cylinder = 6;
    
    	cars[2].year = 1999;
    	cars[2].cylinder = 6;
    
    	return cars;
    }
    
    void sort(Car *cars, int size)
    {
       for (int i = 0; i < size-1; i++)
           for (int j = 0; j < size-i-1; j++)
               if (cars[j].year > cars[j+1].year)
    	   {
    		   Car temp = cars[j];
    		   cars[j] = cars[j+1];
    		   cars[j+1] = temp;
    	   }
    }
    
    void display(Car *cars, int size)
    {
    	for(int i = 0; i < size; i++)
    	{
    		printf("Year: %d\n", cars[i].year);
    		printf("Cylinder: %d\n\n", cars[i].cylinder);
    	}
    }
    
    int main()
    {
    	Car *cars;
    
    	cars = getCars();
    
    	display(cars, 3);
    
    	sort(cars, 3);
    
    	display(cars, 3);
    
    	return 0;
    }
    		
    Struct Composition
    #ifndef WHEEL_H
    #define WHEEL_H
    
    typedef struct{
    	float price;
    } Wheel;
    
    #endif
    		
    #ifndef CAR_H
    #define CAR_H
    #include "Wheel.h"
    
    typedef struct{
    	Wheel wheel;
    	int year;
    	int cylinder;
    } Car;
    
    #endif
    		
    #include <stdio.h>
    #include <stdlib.h>
    #include "Car.h"
    
    int main()
    {
    	Car car;
    
    	car.wheel.price = 100;
    	car.year = 2016;
    	car.cylinder = 4;
    
    	printf("Year: %d\n", car.year);
    	printf("Cylinder: %d\n", car.cylinder);
    	printf("Wheel price: %.2f\n", car.wheel.price);
    
    	return 0;
    }
    		
    Array in Struct
    // Student.h
    #ifndef STUDENT_H
    #define STUDENT_H
    
    typedef struct
    {
    	char name[20];
    	int grades[5];
    } Student;
    
    #endif
    		
    #include <stdio.h>
    #include <string.h>
    #include "Student.h"
    
    int getGrade()
    {
    	int grade;
    	printf("Entery grade:");
    	scanf("%d", &grade);
    
    	return grade;
    }
    
    int main()
    {
    	Student s;
    
    	strcpy(s.name, "Lin"); // why not s.name = "Lin"
    	for(int i = 0; i < 5; i++)
    		s.grades[i] = getGrade();
    	
    	printf("Name: %s\n", s.name);
    	for(int i = 0; i < 5; i++)
    		printf("Grade: %d\n", s.grades[i]);
    
    	return 0;
    }
    		
    Pointer Member
    #ifndef STUDENT_H
    #define STUDENT_H
    
    typedef struct
    {
    	int num; // number of exames
    	int *grades;
    } Student;
    
    #endif
    		
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "Student.h"
    
    void display(Student s)
    {
    	for(int i = 0; i < s.num; i++)
    		printf("Grade: %d\n", s.grades[i]);
    }
    
    int main()
    {
    	Student s;
    	s.num = 3;
    	s.grades = (int *) malloc(s.num*sizeof(int));
    	s.grades[0] = 100;
    	s.grades[1] = 95;
    	s.grades[2] = 90;
    	display(s);
    
    	Student s2;
    	s2 = s;
    	display(s2);
    
    	s.grades[0] = 60;
    	display(s2);
    
    	return 0;
    }
    		

    #ifndef STUDENT_H
    #define STUDENT_H
    
    typedef struct
    {
    	int num; // number of exames
    	int *grades;
    } Student;
    
    #endif
    		
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "Student.h"
    
    void display(Student s)
    {
    	for(int i = 0; i < s.num; i++)
    		printf("Grade: %d\n", s.grades[i]);
    }
    
    void setS(Student *s)
    {
    	s->num = 3;
    	s->grades = (int *) malloc(s->num*sizeof(int));
    	s->grades[0] = 100;
    	s->grades[1] = 95;
    	s->grades[2] = 90;
    }
    
    void copy(Student *dest, Student *source)
    {
    	dest->num = source->num;
    	dest->grades = (int *) malloc(source->num*sizeof(int));
    	memcpy(dest->grades, source->grades, source->num*sizeof(int));
    }
    
    int main()
    {
    	Student s;
    	setS(&s);
    	display(s);
    
    	Student s2;
    	copy(&s2, &s);
    	display(s2);
    
    	s.grades[0] = 60;
    	display(s2);
    
    	return 0;
    }
    		

    Union
    #include 
    #include 
     
    union Employee
    {
    	// age and hours share the same memory
    	int age;
    	int hours;
    };
     
    int main()
    {
    	union Employee e;
     
    	e.age = 39;
    	e.hours = 40;
     
    	printf("%d: %d\n", e.hours, e.age); // 40 40
     
    	return 0;
    }