Title: CS113 Introduction to C
1CS113Introduction to C
- Instructor Ioannis A. VetsikasE-mail
vetsikas_at_cs.cornell.edu - Lecture 6 September 6
2Create your own types typedef
- Syntax typedef ltsome typegt lttype namegt
- Way to remember it typedef ltsame as a
declarationgt
define N 3 typedef double scalar typedef
scalar vectorN typedef scalar
matrixNN void add_vectors( vector x, vector
y, vector z ) int i for( i 0 i lt N
i ) xi yi zi
3Structures
- The structure mechanism allows us to aggregate
elements of different types - Think of objects (but not quite!)
- Your struct definition generally goes outside all
functions
struct card_struct int pips char
suit typedef struct card_struct card
struct card_struct int pips char
suit c1, c2 typedef struct card_struct
card
void some_function() struct card_struct
a card b / a, b have same types / b.pips
3
4Structures (continued)
- Syntax
- struct ltname/taggt
- component-declarations
- ltvariable name(s)gt
- Access elements/fields with dot operator
- b.pips or a.suit
- Structure Names/Tags
- If a structure type does not have a name, then
only a finite number of structures can be created - Structure tags form their own namespace!
- Structure fields also form their own namespace
5Structures (continued)
- Once a structure is declared you can use it as
though it were a default data type (e.g. int) - struct point int x,y
- struct rect struct point pt1, pt2
- Can return from and also pass structures to
functions (call-by-value) - int ptinrect (struct point p, struct point r)
- return p.xgtr.pt1.x p.xltr.pt2.x
- p.ygtr.pt1.y p.yltr.pt2.y
6Structures (continued)
- Another example of a function
- struct point midpoint (struct point a, struct
point b) -
- struct m (a.xb.x)/2, (a.yb.y)/2
- return m
-
- Usually one uses typedef to name the struct in
some way and thus does not have to put struct
lttaggt all the time - What does ab do, when we have defined
- struct point ab ?
- Can also define pointers to structures in the
same way as for simple data types - struct point pt, point_ptr
- We can use the , operators in the same way as
before - point_ptr or pt
7Structures accessing via pointer
- If p is a pointer to a structure and x is an
element of the structure then to access this
element one puts (p).x or more commonly p-gtx
struct card_struct int pips char
suit typedef struct card_struct card void
set_card( card c ) c-gtpips 3
c-gtsuit A void main() card a
set_card( a )
8Pointers to Structures (continued)
- Call by value expensive (and slow) to pass
structures between functions - Use pointer instead (this simulates call by
reference) - Operators -gt and . are left-to-right associative
and have maximum precedence along with () and - e.g. pp-gtx increments field x, not pointer pp
9Structures initialize
- You may initialize a variable corresponding to a
structure that was defined by initializing all
its elements as follows - struct name var init_element_1, ,
init_element_n
include ltstdio.hgt struct address_struct char
street char city_and_state long
zip_code typedef struct address_struct
address void main() address a "1449
Crosby Drive", "Fort Washington, PA", 19034
10Unions
- Similar to structures, but they can only hold one
of the elements at a time - So they use the same spot in memory to save any
of the possible elements. - Memory for union is max of memory needed for each
element
union int_or_float int i float f union
int_or_float a / need to keep track of, on
own, which type is held / / a.i always an int,
a.f always a float /
11sizeof Operator
- sizeof expression/object or sizeof lttype namegt
returns the size of the type of the expression or
the type in bytes - the expression is not evaluated
- E.g. sizeof a, sizeof a13 (a is array) or
sizeof int - Typing sizeof a/sizeof a0 returns?
- Cannot be applied to functions
12Read from KR
Other issues
- Homework 2 due on Friday
- You MUST print the output exactly in the same way
as in the examples which are given - Do not forget to include your name and student
ID when you submit your programs by email give
me printout