E N D
CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 4: More C: Enums, Structs, & Unions Hank Childs, University of Oregon April 11th, 2014
Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)
Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)
Announcement: HW • I am slowing down Proj 1C, 2A, 2B timelines • Proj 1C: now due Sun, not Sat • Proj 2A: now due Weds, not Monday • Proj 2B: now due Sat 4/19, not Fri 4/18. • (please start on it sooner)
Announcements • Matt OH: Mon/Tues 4-5:30, DES 232 • Pavel OH: Weds/Thurs 4-5, DES 100 My Thurs OH: 10:30-12 I am very sorry to anyone inconvenienced. My Fri OH: whoops. New Fri OH: 12:30-1:30?
-I / #include • What is #include for? • What is –I? • Have you ever seen • #include <header.h> • #include “header.h” In –I path In current directory math.h and math330.h are different files
Project 1B • Arguments: • $# • $1 • ./proj1 /tmp • (inside your script, $1=/tmp)
Makefileexample: multiplier lib Causes other rules to get executed too First rule in file is executed You can tell Makefile to execute rule besides first one: “make doubler.o” How does this help with libraries, includes, installs?
Great question from last time… How does this affect Makefiles?
Unix command: tar • tar cvf 330.tar file1 file2 file3 • scp 330.tar @ix:~ • ssh ix • tar xvf 330.tar • ls file1 file2 file
Standard Streams • Wikipedia: “preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution” • Three standard streams: • stdin(standard input) • stdout (standard output) • stderr (standard error) What mechanisms in C allow you to access standard streams?
pipes in Unix shells • represented with “|” • output of one program becomes input to another program
Questions? • Something unclear from last time? • Questions about HW? • Is everybody on Piazza? • vi tips?
Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)
Project 2A Binary file on web is now corrected. (Thank you Morgan!) 1 0
Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)
Enums • Enums make your own type • Type is “list of key words” • Enums are useful for code clarity • Always possible to do the same thing with integers • Be careful with enums • … you can “contaminate” a bunch of useful words
enum example C keyword “enum” – means enum definition is coming This enum contains 6 different student types semi-colon!!!
But enums can be easier to maintain than integers If you had used integers, then this is a bigger change and likely to lead to bugs.
Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)
Data types • float • double • int • char • unsigned char All of these are simple data types
Structs: a complex data type • Construct that defines a group of variables • Variables must be grouped together in contiguous memory • Also makes accessing variables easier … they are all part of the same grouping (the struct)
struct syntax “.” accesses data members for a struct C keyword “struct” – means struct definition is coming This struct contains 6 doubles, meaning it is 48 bytes semi-colon!!! Declaring an instance
Nested structs accesses dir part of Ray accesses directionZ part of Direction (part of Ray)
typedef • typedef: tell compiler you want to define a new type saves you from having to type “struct” every time you declare a struct.
Other uses for typedef • Declare a new type for code clarity • typedefintMilesPerHour; • Makes a new type called MilesPerHour. • MilesPerHour works exactly like an int. • Also used for enums & unions • same trick as for structs … typedef saves you a word
Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)
Unions • Union: special data type • store many different memory types in one memory location When dealing with this union, you can treat it as a float, as an int, or as 4 characters. This data structure has 4 bytes
Unions Why are unions useful?
Why are Unions useful? • Allows you to represent multiple data types simultaneously • But only if you know you want exactly one of them • Benefit is space efficiency, which leads to performance efficiency Unions are also useful for abstracting type. We will re-visit this when we talk about C++’s templates.
Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)
Project 2B • Prompt posted tonight, Saturday (8 days) • Define structs, union of structs, functions that manipulate structs
Outline • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)
Problem with C… No checking of type…
Mangling • Mangling refers to combing information about arguments and “mangling” it with function name. • Way of ensuring that you don’t mix up functions. • Why not return type too? • Causes problems with compiler mismatches • C++ compilers haven’t standardized. • Can’t take library from icpc and combine it with g++.
C++ also gives you access to mangling via “namespaces” Functions or variables within a namespace are accessed with “::”
C++ also gives you access to mangling via “namespaces” The “using” keyword makes all functions and variables from a namespace available without needing “::”. And you can still access other namespaces.
Outline • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)
Killing programs, part 1 • If you want to kill a program that your shell is currently running, then it is easy: • press Control-C • What happens? • OS will send a signal to the program. • Programs can catch the signal and ignore, but most programs just die.