Introduction to C Complex Types Instructor Yin Lou 02 02 2011 Introduction to C CS 2022 Spring 2011 Lecture 5 Recap Numerical Types I Data Types I I I I int machine dependent float 32 bit 4 bytes double 64 bit 8 bytes char 8 bit 1 byte Introduction to C CS 2022 Spring 2011 Lecture 5 Recap Numerical Types I Data Types I I I I I int machine dependent float 32 bit 4 bytes double 64 bit 8 bytes char 8 bit 1 byte Modifiers I I I I short long signed unsigned Introduction to C CS 2022 Spring 2011 Lecture 5 Recap Numerical Types I Data Types I I I I I Modifiers I I I I I int machine dependent float 32 bit 4 bytes double 64 bit 8 bytes char 8 bit 1 byte short long signed unsigned Example short int unsigned char long double etc Introduction to C CS 2022 Spring 2011 Lecture 5 Complex Types I Enumerations I I I I User defined weekday sunday monday Structures User defined combinations of other types Unions I Same data multiple interpretations I Function pointers I Arrays and pointers of the above Introduction to C CS 2022 Spring 2011 Lecture 5 Enumerations enum days mon tue wed thu fri sat sun Same as define mon 0 define tue 1 define sun 6 enum days mon Same as define mon define tue define sun 3 tue 8 wed thu fri sat sun 3 8 13 Introduction to C CS 2022 Spring 2011 Lecture 5 Enumerations enum days day Same as int day for day mon day sun day if day sun printf Sun n else printf day d n day Introduction to C CS 2022 Spring 2011 Lecture 5 Enumerations I Basically integers I Can use in expressions like this I Makes code easier to read I Cannot get string equiv I Caution day will always add 1 even if enum values aren t contiguous Introduction to C CS 2022 Spring 2011 Lecture 5 Creating Your Own Types I C lets you name your own types using typedef Introduction to C CS 2022 Spring 2011 Lecture 5 Creating Your Own Types I C lets you name your own types using typedef Example typedef double scalar scalar add scalar a scalar b return a b Introduction to C CS 2022 Spring 2011 Lecture 5 Creating Your Own Types I C lets you name your own types using typedef Example typedef double scalar scalar add scalar a scalar b return a b I typedef syntax I I I As if you re declaring a variable of the type you want to redefine Give the variable the name of the new type Put typedef in front Introduction to C CS 2022 Spring 2011 Lecture 5 More on typedef typedef double scalar typedef scalar vector 10 scalar add scalars scalar a scalar b return a b void add vectors vector result vector a vector b int i for i 0 i 10 i result i a i b i Introduction to C CS 2022 Spring 2011 Lecture 5 Structures I Structures aggregate variables of different types I Defined with the keyword struct Introduction to C CS 2022 Spring 2011 Lecture 5 Structures I Structures aggregate variables of different types I Defined with the keyword struct Example struct student int id int year char grade void main struct student s s id 10001 s year 2011 s grade A Introduction to C CS 2022 Spring 2011 Lecture 5 Structure Syntax I To define a structure Introduction to C CS 2022 Spring 2011 Lecture 5 Structure Syntax I To define a structure Definition struct structure name list of variable declarations Introduction to C CS 2022 Spring 2011 Lecture 5 Structure Syntax I To define a structure Definition struct structure name list of variable declarations I To declare a variable of the new structure type Introduction to C CS 2022 Spring 2011 Lecture 5 Structure Syntax I To define a structure Definition struct structure name list of variable declarations I To declare a variable of the new structure type Example void main struct structure name variable name Introduction to C CS 2022 Spring 2011 Lecture 5 Structure Syntax I To define a structure Definition struct structure name list of variable declarations I To declare a variable of the new structure type Example void main struct structure name variable name I The struct keyword is required in both places Introduction to C CS 2022 Spring 2011 Lecture 5 Structures and typedef I To avoid the struct keyword use a typedef I Create alias for struct student called student t Introduction to C CS 2022 Spring 2011 Lecture 5 Structures and typedef I To avoid the struct keyword use a typedef I Create alias for struct student called student t Example struct student int id int year char grade typedef struct student student t void main student t s s id 10001 s year 2011 s grade A Introduction to C CS 2022 Spring 2011 Lecture 5 Another Example 2 D Points a shortcut to define structure typedef struct double x double y point double distance point p1 point p2 return p1 x p2 x p1 x p2 x p1 y p2 y p1 y p2 y void main point a 12 0 42 0 point b 15 0 36 0 Introduction to C CS 2022 Spring 2011 Lecture 5 Structures in C I C treats structures as if they were primitive data types Introduction to C CS 2022 Spring 2011 Lecture 5 Structures in C I C treats structures as if they were primitive data types I I I i e not like reference types in Java Passing a structure as an argument to a function means that a temporary copy is made of the entire structure Assignments do a deep copy of the entire structure Introduction to C CS 2022 Spring 2011 Lecture 5 Structures in C I C treats structures as if they were primitive data types I I I i e not like reference types in Java Passing a structure as an argument to a function means that a temporary copy is made of the entire structure Assignments do a deep copy of the entire structure Example void reset point p p x p y 0 void main point a 12 0 42 0 point b a reset a b x 0 printf a lf lf n a x a y a 12 0 42 0 printf b lf lf n b x b y b 0 42 0 Introduction to C CS 2022 Spring 2011 Lecture 5 Structures and Function Calls I Function calls with structure arguments can be expensive Introduction to C CS 2022 Spring 2011 Lecture 5 Structures and Function Calls I Function calls with structure arguments can be expensive 10000 bytes must be copied typedef struct char name 10000 int id person void print person person p printf d s n p id p name void main person a Mike Smith 1234 print person a Introduction to C CS 2022 Spring 2011 Lecture 5 Solution I Use pointers Only the address of the structure is copied not the …
View Full Document
Unlocking...