{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS2_43_C_Part2

# CS2_43_C_Part2 - CS2 Module 43 Category Elements of C Topic...

This preview shows pages 1–13. Sign up to view the full content.

CS2 Module 43 Category: Elements of C Topic: Part 2 Objectives Pointers Structs Self-referential Memory allocation

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 2 Introduction to Object Oriented Programming Module 43 Elements of C Part II
Put Some Structure in Your Life C uses a struct to group one or more variables into a single unit Equivalent to a record in Pascal Equivalent to a class with no methods ( the horror. .. ) in Java Equivalent to structures in Scheme Example: struct point { struct point { int x; int x; int y; int y; }; };

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Structures The preceding example was a declaration, that is, it essentially creates a new type. In this case we included the word “point” which is known as a structure tag Now we can say: struct point { struct point { int x; int x; int y; int y; }; }; struct point pt1; struct point pt1; struct point pt2; struct point pt2; pt1.x = 5; pt1.x = 5; pt1.y = 7; pt1.y = 7; pt2 = pt1; pt2 = pt1;
Structures We could even say struct { int x; int y; } pt3, pt4; Note that while pt2 = pt1; pt3 = pt4; both work pt3 = pt2; does not! struct point { struct point { int x; int x; int y; int y; }; }; struct point pt1; struct point pt1; struct point pt2; struct point pt2; pt1.x = 5; pt1.x = 5; pt1.y = 7; pt1.y = 7; pt2 = pt1; pt2 = pt1; Known as an "anonymous type" Bad style?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Structures Structures can be copied or assigned can be passed to and returned from functions struct point doublePoint(struct point in) { struct point temp; temp.x = 2 * in.x; temp.y = 2 * in.y; return temp; } may be initialized: struct point maxpt = {320, 200}; struct point maxpt = {320, 200}; can be used in arrays (arrays of structs)
Self-Referential Structures Self-referential simply means a structure that contains a reference or pointer to a structure of its own type. struct lnode { struct lnode { int data; int data; struct lnode *next; struct lnode *next; } } More on pointers soon

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Roll Your Own! For readability and convenience it would be nice to be able to make up synonyms for existing types The typedef typedef keyword provides this facility Example: typedef int Length; Now saying Length x, y, z; is the same as saying int x, y, z;
Putting it all together typedef struct point { int x; int y; } point_type ; Or even typedef struct point { int x; int y; } point ; So now we can say: point p1, p2; C keeps these names in separate spaces

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Watch closely!!! typedef struct lnode { point data; /* Nested structure */ struct lnode *next; } lnode ; lnode head;
Another common way!!! typedef struct lnode_tag { point data; /* Nested structure */ struct lnode_tag *next; } lnode ; lnode head;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Structs One More Time Confused? This might help. The syntax of a typedef is as follows: typedef <type> <handle> Thus: typedef int myType; If the ‘type’ that we typedef is a struct, we get: typedef struct { . .. } myTemp;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 65

CS2_43_C_Part2 - CS2 Module 43 Category Elements of C Topic...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online