CS2_43_C_Part2

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

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

View Full Document Right Arrow Icon
CS2 Module 43 Category: Elements of C Topic: Part 2 Objectives Pointers Structs Self-referential Memory allocation
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 43 Elements of C Part II
Background image of page 2
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; }; };
Background image of page 3

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

View Full DocumentRight Arrow Icon
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;
Background image of page 4
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?
Background image of page 5

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

View Full DocumentRight Arrow Icon
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)
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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;
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
Watch closely!!! typedef struct lnode { point data; /* Nested structure */ struct lnode *next; } lnode ; lnode head;
Background image of page 10
Another common way!!! typedef struct lnode_tag { point data; /* Nested structure */ struct lnode_tag *next; } lnode ; lnode head;
Background image of page 11

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

View Full DocumentRight Arrow Icon
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;
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 65

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online