c structs n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
C structs PowerPoint Presentation
Download Presentation
C structs

Loading in 2 Seconds...

play fullscreen
1 / 10

C structs - PowerPoint PPT Presentation


  • 115 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'C structs' - rollin


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
c structs

C structs

Complex datatypes

CE-2810

Dr. Mark L. Hornick

so far we ve only discussed primitive c data types
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

suppose we want to declare a complex datatype to represent a point cartesian
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

a struct can be initialized in a couple of ways
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

suppose we want to declare a rectangle that contains two points
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

a rect can be initialized in a couple of ways
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

declaration of a pointer to a struct
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

accessing struct elements via a struct pointer
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

typedefs aliases for datatypes
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

typedefs aliases for datatypes1
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