CS157: Intro to C, Part II

Spring 2018

Struct 1

See this page as a slide show

User Defined Datatypes

CS157 Struct1

User Defined Datatypes

Structs

// Declare a person struct
struct person {
    char name[50];
    int age;
    char phone[15];
    float height;
};

Structs

FieldValueTypeSize
nameJack Applinchar [50]50 bytes
age58intprobably 4 bytes
phone970-555-1212char [15]15 bytes
height5.75floatprobably 4 bytes

Each field has its own memory location.

// Declare a person struct
struct person {
    char name[50];
    int age;
    char phone[15];
    float height;
};

Structs

// Declare a person struct
struct person {
    char name[50];
    int age;
    char phone[15];
    float height;
};
struct person fritz;
struct person wim, darryl;
struct person teachers[20];

Structs

// Declare a person struct
struct person {
    char name[50];
    int age;
    char phone[15];
    float height;
};

struct person liz;
liz.age = 13;
strcpy(liz.name, "Lizzie");

Work with variables the same way as regular variables of a particular data type.

Structs: Example

FieldValueType
nameGeorge W. Shrubchar [50]
age103int
height4.5float
struct person {
    char name[50];
    int age;
    float height;
};

int main() {
    struct person who;
    strcpy(who.name, "George W. Shrub");
    who.age = 103;
    who.height = 4.5;
    printf("%s is %d years old\n",
        who.name, who.age);
    return 0;
}

Struct

Various ways to create a structure

// Defining both a new data type and variables:
struct Date {
    int month, day;
} birth, current;
struct Date superbowl;
birth.month = 11;
// Defining a new data type:
struct Date {
    int month, day;
};
struct Date now;
now.month = 5;
// Defining variables, but no type:
struct {
    int month, day;
} birth, current;
// Cannot make any new variables!
birth.month = 11;

Struct

Initializer list (order is important!)

#include <stdio.h>

struct Date {
   int month, day, year;    // Crazy American order
};

int main() {
   struct Date dad = {8, 1, 1922};
   printf("Happy birthday, Dad: %02d/%02d/%d\n",
          dad.month, dad.day, dad.year);
   return 0;
}
Happy birthday, Dad: 08/01/1922

Structures

Passing to functions

Struct

We can send structures to functions

#include <stdio.h>
struct Date {
   int month, day;
};

void modify(struct Date val) {
   val.month = 1;
   val.day = 28;
}

int main() {
   struct Date birth = {7, 4};
   printf("Before: %02d/%02d\n", birth.month, birth.day);
   modify(birth);
   printf("After:  %02d/%02d\n", birth.month, birth.day);
   return 0;
}
Before: 07/04
After:  07/04

Struct — Version 2

We can refer to globally defined structures

#include <stdio.h>
struct Date {
   int month, day;
} birth = {7, 4};

void modify() {
   birth.month = 1;
   birth.day = 28;
}

int main() {
   printf("Before: %02d/%02d\n", birth.month, birth.day);
   modify();
   printf("After:  %02d/%02d\n", birth.month, birth.day);
   return 0;
}
Before: 07/04
After:  01/28

Struct — Version 3

We can send a pointer to the structure

#include <stdio.h>
struct Date {
   int month, day;
};

void modify(struct Date *p) {
   p->month = 1;
   p->day = 28;
}

int main() {
   struct Date birth = {7, 4};
   printf("Before: %02d/%02d\n", birth.month, birth.day);
   modify(&birth);
   printf("After:  %02d/%02d\n", birth.month, birth.day);
   return 0;
}
Before: 07/04
After:  01/28

Structures

Arrays

Arrays of Structs

We can create arrays of structs just like any other type.

int main() {
  struct person people[3];
  strcpy(people[0].name, "Jack");
  people[0].height = 5.5
  strcpy(people[2].name, "Pierre");
  people[2].age = 54;
  ...etc...
}
nameJack namenamePierre
age?age?age54
phone?phone?phone?
height5.5height?height?

Struct array

#include <stdio.h>
#include <string.h>
struct student { 
  char   name[50];
  int    age;
};
#define NUM_STUDENTS 3
int main() {
    struct student cs157[NUM_STUDENTS] = {
        {"Bob", 84}, {"Rob", 14}, {"Zob", 4},
    };
    for (int i=0; i<NUM_STUDENTS; i++)
        printf("Student %d: %s %d years old\n", 
                i+1, cs157[i].name, cs157[i].age);
   return 0;
}
Student 1: Bob 84 years old
Student 2: Rob 14 years old
Student 3: Zob 4 years old

Why use structs?

Unions

Unions

Unions

// Declare a union
union int_or_float {      // tag name
    int i;                // one field
    float f;              // another field
};

Unions

// Declare a union
union int_or_float {      // tag name
    int i;                // one field
    float f;              // another field
};

int main() {
    union int_or_float q;
    q.i = 14;
    q.f = 175.543;
    q.i = q.f;
    return 0;
}

Unions

Enumerated Types

Enumerated Types

Enum

// Declare a enum
enum traffic_light { // tag name
    red,             // value
    yellow,          // value
    green            // value
};

values

enum Example

enum traffic_light { // tag name
    red,             // value
    yellow,          // value
    green            // value
};

void decide(enum traffic_light light) {
    switch (light) {
        case green:  puts("go");           break;
        case red:    puts("stop");         break;
        case yellow: puts("go very fast"); break;
    }
}

enum — passing to functions

Types

Tag Names and Declarations

type tag {
    ...
} var1, var2, var3, ...;

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-09T12:30

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building