Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6.

Similar presentations


Presentation on theme: "CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6."— Presentation transcript:

1 CS113 Introduction to C Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.eduvetsikas@cs.cornell.edu Lecture 6 : September 6

2 2 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 Way to remember it: typedef

3 3 Structures struct card_struct { int pips; char suit; }; typedef struct card_struct card; 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; } c1, c2; typedef struct card_struct card; void some_function() { struct card_struct a; card b; /* a, b have same types */ b.pips = 3; }

4 4 Structures (continued) Syntax: struct [ ] { component-declarations } [ ]; 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…

5 5 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 }

6 6 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 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

7 7 Structures – 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->x 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 ); }

8 8 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

9 9 Structures – 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 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 }; }

10 10 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 */

11 11 sizeof Operator sizeof expression/object or sizeof 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

12 12 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


Download ppt "CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6."

Similar presentations


Ads by Google