1 / 10

C structs

C structs. Complex datatypes. So far, we’ve only discussed primitive C data types. A struct is a complex datatype that can consist of Primitive datatypes Ints, floats, chars Arrays of any of the above Pointers to primitive datatypes Other structs or pointers to other structs.

rollin
Download Presentation

C structs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C structs Complex datatypes CE-2810 Dr. Mark L. Hornick

  2. So far, we’ve only discussed primitive C data types A struct is a complex datatype that can consist of • Primitive datatypes • Ints, floats, chars • Arrays of any of the above • Pointers to primitive datatypes • Other structs • or pointers to other structs CS-280 Dr. Mark L. Hornick

  3. Suppose we want to declare a complex datatype to represent a Point (cartesian) struct Point { // declaration of Point float x; // x coordinate float y; // y coordinate }; To define a variable that is a Point, we write: struct Point point1; // unitialized struct Point point2; // unitialized The declaration is typically placed outside function bodies. CE-2810 Dr. Mark L. Hornick

  4. A struct can be initialized in a couple of ways To define and initialize a variable that is a Point, we write either: struct Point point1 = {1.0,2.0}; struct Point point2; // uninitialized point2.x = 3.0; point2.y = 4.0; The period is known as the structure member operator, Similar to the dot operator used in Java. CE-2810 Dr. Mark L. Hornick

  5. Suppose we want to declare a Rectangle that contains two Points struct Rect{ // declaration of Rect struct Point p1; // upper left struct Point p2; // lower right }; To define a variable that is a Rect, we write: struct Rect r1; struct Rect r2; CE-2810 Dr. Mark L. Hornick

  6. A Rect can be initialized in a couple of ways To define and initialize a variable that is a Rect, we write: struct Rect r1 = { {1,2}, {3,4} }; Struct Rect r2; // unitialized r2.p1.x=1.0; r2.p1.y=2.0; r2.p2.x=3.0; r2.p2.y=4.0; CE-2810 Dr. Mark L. Hornick

  7. Declaration of a pointer to a struct First, say we have a Rect: struct Rect r1 = {{1,2},{3,4}}; To define and initialize a variable that is a pointer to Rect, we write: struct Rect* pRect = &r1; CE-2810 Dr. Mark L. Hornick

  8. Accessing struct elements via a struct pointer struct Rect r1; // uninitialized struct Rect* pRect = &r1; pRect->p1.x=1.0; pRect->p1.y=2.0; pRect->p2.x=3.0; pRect->p2.y=4.0; The “->” bigraph is the structure member operator you usewhen you have a pointer to a struct. CE-2810 Dr. Mark L. Hornick

  9. typedefs – aliases for datatypes • The C typedef statement allows you to do things like this: typedef float realnum ; // realnum is now a defined type realnum x; // define a variable of type realnum typedef float* PFLOAT; // PFLOAT is now a defined type PFLOAT px; // define a variable of type PFLOAT px = &x; CS-280 Dr. Mark L. Hornick

  10. typedefs – aliases for datatypes • The C typedef statement allows you to do things like this: typedef struct Point { // declaration of Point struct float x; // x coordinate float y; // y coordinate } Point; // definition of Point datatype typedef *Point POINTREF; // definition of POINTREF type Point pt1; // “object” of type Point POINTREF pPt1 = & pt1; // pointer to a Point object CS-280 Dr. Mark L. Hornick

More Related