Week 10 - Monday What did we talk about last time? structs C - - PowerPoint PPT Presentation

week 10 monday what did we talk about last time structs c
SMART_READER_LITE
LIVE PREVIEW

Week 10 - Monday What did we talk about last time? structs C - - PowerPoint PPT Presentation

Week 10 - Monday What did we talk about last time? structs C combines the power and performance of assembly language with the flexibility and ease-of-use of assembly language. Anonymous C structs are similar to Java classes designed


slide-1
SLIDE 1

Week 10 - Monday

slide-2
SLIDE 2

 What did we talk about last time?  structs

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

C combines the power and performance of assembly language with the flexibility and ease-of-use of assembly language. Anonymous

slide-6
SLIDE 6

 C structs are similar to Java classes designed to hold data

  • Just remember to put a semicolon after the struct declaration

 A string can either be a char* (the memory for it is allocated elsewhere)

  • r a char array with a maximum size

 Examples:

struct point { double x; double y; }; struct student { char name[100]; double GPA; int ID; };

A struct to hold a point in space A struct to hold student data

slide-7
SLIDE 7

 Type:

  • struct
  • The name of the struct
  • The name of the identifier

 You have to put struct first!

struct student bob; struct student jameel; struct point start; struct point end;

slide-8
SLIDE 8

 Once you have a struct variable, you can access its members

with dot notation (variable.member)

  • Members can be read and written

struct student bob; strcpy(bob.name, "Bob Blobberwob"); bob.GPA = 3.7; bob.ID = 100008; printf("Bob's GPA: %f\n", bob.GPA);

slide-9
SLIDE 9

 There are no constructors for structs in C  You can initialize each element manually:  Or you can use braces to initialize the entire struct at once:

struct student julio; strcpy(julio.name, "Julio Iglesias"); julio.GPA = 3.9; julio.ID = 100009;

struct student julio = { "Julio Iglesias", 3.9, 100009 };

slide-10
SLIDE 10

 It is possible to assign one struct to another  Doing so is equivalent to using memcpy() to copy the memory of julio

into the memory of bob

 bob is still separate memory: it's not like copying references in Java

struct student julio; struct student bob; strcpy(julio.name, "Julio Iglesias"); julio.GPA = 3.9; julio.ID = 100009; bob = julio;

slide-11
SLIDE 11

 It is perfectly legal to put arrays of values, pointers, and even other

struct variables inside of a struct declaration

 If it's a pointer, you will have to point it to valid memory yourself

struct point { double x; double y; }; struct triangle { struct point vertices[3]; };

slide-12
SLIDE 12

 With a pointer in a struct, copying the struct will copy the pointer but will not make a copy

  • f the contents

 Changing one struct could change another

bob1.firstName = strdup("Bob"); bob1.lastName = strdup("Newhart"); bob2 = bob1; strcpy(bob2.lastName, "Hope"); printf("Name: %s %s\n", bob1.firstName, bob1.lastName); // Prints Bob Hope

struct person { char* firstName; char* lastName; }; struct person bob1; struct person bob2;

slide-13
SLIDE 13

 An array of structs is common

  • Student roster
  • List of points

 Like any other array, you put the name of the type (struct

name) before the variable, followed by brackets with a fixed size

 An array of structs is filled with uninitialized structs whose

members are garbage

struct student students[100];

slide-14
SLIDE 14

 Similarly, we can define a pointer to a struct variable

  • We can point it at an existing struct
  • We can dynamically allocate a struct to point it at
  • This is how linked lists are going to work

struct student bob; struct student* studentPointer; strcpy(bob.name, "Bob Blobberwob"); bob.GPA = 3.7; bob.ID = 100008; studentPointer = &bob; (*studentPointer).GPA = 2.8; studentPointer = (struct student*)malloc(sizeof(struct student));

slide-15
SLIDE 15

 As we saw on the previous slide, we have to dereference a struct

pointer and then use the dot to access a member

 This is cumbersome and requires parentheses  Because this is a frequent operation, dereference + dot can be

written as an arrow (->)

struct student* studentPointer = (struct student*) malloc(sizeof(struct student)); (*studentPointer).ID = 3030; studentPointer->ID = 3030;

slide-16
SLIDE 16

 If you pass a struct directly to a function, you are passing it by

value

  • A copy of its contents is made

 It is common to pass a struct by pointer to avoid copying and

so that its members can be changed

void flip(struct point* value) { double temp = value->x; value->x = value->y; value->y = temp; }

slide-17
SLIDE 17

 Always put a semicolon at the end of a struct declaration  Don't put constructors or methods inside of a struct

  • C doesn't have them

 Assigning one struct to another copies the memory of one

into the other

 Pointers to struct variables are usually passed into functions

  • Both for efficiency and so that you can change the data inside
slide-18
SLIDE 18
slide-19
SLIDE 19

 You might have noticed that there are all these odd types

floating around

  • time_t
  • size_t

 On some systems, you will even see aliases for your basic

types

  • FLOAT
  • INT32

 How do people create new names for existing types?

slide-20
SLIDE 20

 The typedef command allows you to make an alias for an

existing type

 You type typedef, the type you want to alias, and then the

new name

 Don't overuse typedef  It is useful for types like time_t which can have different

meanings in different systems

typedef int SUPER_INT; SUPER_INT value = 3; // Has type int

slide-21
SLIDE 21

 The typedef command is commonly used with structs

  • Often it is built into the struct declaration process

 It allows the programmer to leave off the stupid struct keyword

when declaring variables

 The type defined is actually struct _wombat  We can refer to that type as wombat

typedef struct _wombat { char name[100]; double weight; } wombat;

wombat martin;

slide-22
SLIDE 22

 You can actually typedef the name of the struct to be the same without the

struct part

 Or, if you don't need the name of the struct inside itself, you can typedef an

anonymous struct

typedef struct wombat { char name[100]; double weight; } wombat; typedef struct { char name[100]; double weight; } wombat;

slide-23
SLIDE 23
slide-24
SLIDE 24

 If you took COMP 2100, you know the power of the linked list  A linked list is a dynamic data structure with the following

features:

  • Insertion, add, and delete can be O(1) time
  • Search is O(n) time
  • They are ideally suited for a merge sort
  • They are a pain to program
slide-25
SLIDE 25

 Node consists of data and a single next pointer  Advantages: fast and easy to implement  Disadvantages: forward movement only

X

head 23 47 58

slide-26
SLIDE 26

 Since C doesn't have classes, we can't make a self-contained

linked list

 But we can create nodes and a set of operations to use on

them

 Clearly, we'll need a struct to make the node

  • It will contain data
  • It will contain a pointer to the next node in the list

 Doubly-linked lists are possible too

slide-27
SLIDE 27

 We'll use this definition for our node for singly linked lists  Somewhere, we will have the following variable to hold the beginning of

the list

typedef struct _node { int data; struct _node* next; } node; node* head = NULL;

slide-28
SLIDE 28

 Let's define a function that takes a pointer to a (possibly

empty) linked list and adds a value to the front

 There are two possible ways to do it

  • Return the new head of the list
  • Take a pointer to a pointer and change it directly

node* add(node* head, int value); void add(node** headPointer, int value);

slide-29
SLIDE 29

 Let's define a function that takes a pointer to a (possibly

empty) linked list and a value and returns the node containing the value

  • Or NULL if there is no such node

node* find(node* head, int value);

slide-30
SLIDE 30

 Let's define a function that takes a pointer to a (possibly

empty) linked list and returns the sum of the values inside

  • An empty list has a sum of 0

int sum(node* head);

slide-31
SLIDE 31

 Let's define a function that takes a pointer to a (possibly empty)

linked list and deletes the first occurrence of a given value

  • List is unchanged if the value isn't found

 There are two possible ways to do it

  • Return the new head of the list
  • Take a pointer to a pointer and change it directly

node* remove(node* head, int value); void remove(node** headPointer, int value);

slide-32
SLIDE 32

 Let's define a function that takes a pointer to a (possibly

empty) linked list and adds a value in sorted order (assuming that the list is already sorted)

 There are two possible ways to do it

  • Return the new head of the list
  • Take a pointer to a pointer and change it directly

node* add(node* head, int value); void add(node** headPointer, int value);

slide-33
SLIDE 33
slide-34
SLIDE 34

 Time from the Linux perspective  Deeper coverage of linked lists  First virtual lab is tomorrow

slide-35
SLIDE 35

 Work on Project 4  Keep reading K&R chapter 6