120 likes | 232 Views
CS113 Introduction to C. Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.edu Lecture 6 : September 6. Create your own types: typedef. #define N 3 typedef double scalar; typedef scalar vector[N]; typedef scalar matrix[N][N];
E N D
CS113Introduction to C Instructor: Ioannis A. VetsikasE-mail: vetsikas@cs.cornell.edu Lecture 6 : September 6
Create your own types: typedef #define N 3 typedef double scalar; typedef scalar vector[N]; typedef scalar matrix[N][N]; void add_vectors( vector x, vector y, vector z ) { int i; for( i = 0; i < N; i++ ) { x[i] = y[i] + z[i]; } } Syntax: typedef <some type> <type name> Way to remember it: typedef <same as a declaration>
Structures • 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; }
Structures (continued) • Syntax: struct [<name/tag>] { component-declarations } [<variable name(s)>]; • 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…
Structures (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.x>=r.pt1.x && p.x<r.pt2.x && p.y>=r.pt1.y && p.y<r.pt2.y }
Structures (continued) • Another example of a function: struct point midpoint (struct point a, struct point b) { struct m = {(a.x+b.x)/2, (a.y+b.y)/2}; return m; } • Usually one uses typedef to name the struct in some way and thus does not have to put struct <tag> all the time • What does a=b do, when we have defined struct point a=b ? • 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
Structures – accessing via pointer struct card_struct { int pips; char suit; }; typedef struct card_struct card; void set_card( card *c ) { c->pips = 3; c->suit = ‘A’; } void main() { card a; set_card( &a ); } 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->x
Pointers to Structures (continued) • Call by value: expensive (and slow) to pass structures between functions • Use pointer instead (this simulates call by reference) • Operators -> and . are left-to-right associative and have maximum precedence along with () and [] • e.g. ++pp->x increments field x, not pointer pp
Structures – initialize #include <stdio.h> 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 }; } 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}
Unions • 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 */
sizeof Operator • sizeof expression/object or sizeof <type name> returns the size of the type of the expression or the type in bytes • the expression is not evaluated • E.g. sizeof a, sizeof a[13] (a is array) or sizeof int • Typing sizeof a/sizeof a[0] returns? • Cannot be applied to functions
Read from K&R • Covered 6.1-6.4 today 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