Programming in C

Typedef and Structs - Composite Data Types

Gerald Senarclens de Grancy

Purpose of Structs

Which problems does a struct solve?

  • Return multiple values from a function
xG=13(xA+xB+xC) yG=13(yA+yB+yC)
[centroid of a triangle]

Example: Calculate the centroid of a triangle.

def centroid(x_A, y_A, x_B, y_B, x_C, y_C):
x_G = 1 / 3 * (x_A + x_B + x_C)
y_G = 1 / 3 * (y_A + y_B + y_C)
return x_G, y_G

Without structs, returning more than one value is not possible in C.

Store a Record of Data

struct is just another word for "record"

A data table can be stored as an array of structs.

Shopping List
Apples2kg
Lemonade4l
Bread1kg

Definition

  • A record (struct) is a basic data structure
    • In database / spreadsheet software, a record is usually called a row
  • Physically grouped list of variables under one name in a block of memory
  • Can store mixed-data-type records
  • Block of contiguous memory
  • In C++, a struct is identical to a class but has different default visibility
  • Struct (C programming language)

Declaring and Defining Structs

Declaration

struct struct_name {
type member1;
type member2;
/* declare as many members as desired */
};

Example: Declaration and Definition of a Date

struct date {
int year;
int month;
int day;
};
struct date d = { 2021, 12, 10 };
The members can also be named explicitly:
struct date {
int year;
int month;
int day;
};
struct date d = { .year=2021, .month=12, .day=10 };

source/c/structs/date.c

Typedef

  • Used to create an additional name (alias) for another data type
  • Particularly useful in combination with structs
typedef type_definition new_identifier;

Example: Declaration of Point in a 2 Dimensional Space

struct point {
float x;
float y;
};
struct point p = {1.5, 3.0};
Using typedef improves the readability.
typedef struct point {
float x;
float y;
} Point;
Point p = {1.5, 3.0};

Common Operations

Assignment

source/c/structs/assignment.c

#include <stdio.h>

typedef struct {
float x;
float y;
} Point;

int main() {
Point p1 = { .x=1.5 , .y=3.0 };
Point p2 = p1; // copy assignment
p2.x = -5; // does not affect `p1`
return 0;
}

Passing Structs to Functions

source/c/structs/parameter.c

#include <stdio.h>

typedef struct {
float x;
float y;
} Point;

void print_point(Point p) {
printf("P(%.2f/%.2f)\n", p.x, p.y);
p.x = 0; // does not affect p in main
}

int main() {
Point p = { .x=1.5 , .y=3.0 };
print_point(p); // pass by value
return 0;
}

Returning Structs from Functions

source/c/structs/return.c

#include <stdio.h>

typedef struct {
float x;
float y;
} Point;

Point centroid(Point A, Point B, Point C) {
float x_G = 1.0 / 3.0 * (A.x + B.x + C.x);
float y_G = 1.0 / 3.0 * (A.y + B.y + C.y);
Point G = {x_G, y_G};
return G;
}

int main() {
Point A = {1.5, 3.0}, B = {12.0, 3.0}, C = {4.5, 9.0};
Point G = centroid(A, B, C); // pass by value
return 0;
}

Exercise

  1. Create a typedef struct Date that holds int year, int month and int day.
  2. Define a function print_date(Date d) that uses printf to print a given date to standard output. The date must be printed in ISO 8601 format (YYYY-MM-DD), followed by a newline.
  3. Define today's Date in main and call print_date to print today's date.

Solution

source/c/structs/print_date.c

#include <stdio.h>

typedef struct {
int year;
int month;
int day;
} Date;

void print_date(Date d) {
printf("%04d-%02d-%02d\n", d.year, d.month, d.day);
}

int main() {
Date today = {2021, 12, 9};
print_date(today);
return 0;
}

Questions
and feedback...