Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointer to Structures Lesson xx

Similar presentations


Presentation on theme: "Pointer to Structures Lesson xx"— Presentation transcript:

1 Pointer to Structures Lesson xx
 In this presentation, we introduce you to the concept of pointer to structures.

2 Objectives Review structures Review pointers to basic data types
Program to demonstrate pointer to structures Pointer to structure operator -> Our goal is to review structures, review pointers to basic data types and then show you a program that uses pointer to structures. We will also be learning about the pointer to structure operator.

3 Structure struct time { int h; int m; now }; int main ( ) 2 time now;
now.h = 2; now.m = 17; return 0; } 2 17 now.h now.m now Here is how you set up a structure definition and do a structure declaration. In this piece of code, we have defined a structure called time which has 2 integer data members called h and m. When you want to declare a structure variable, you write the statement: time now. This sets up a structure variable called now. You can see that now.h is initialized to 2 and now.m is initialized to 17. We have drawn a picture of the computer’s memory for you.

4 Pointer to Basic Data Types
int * pi; float * pf; char *pc; double * pd; Here are some example of pointers to basic data types. int * pi; declares pi to be a pointer to an integer. char *pc; declares a pointer to a character. You can have a pointer to any data type you wish. In fact, you can even have a pointer to a structure and this is sometimes called a pointer to a user defined type. Let’s look at a program that demonstrate this.

5 Pointer Structure Program
struct date {   int month;   int day;   int year; }; int main() {   int x;   date today;   int* px;   date* dp;   px = & x;   dp = &today;   *px = 52;   (*dp).day = 21;   return 0; } This is a listing of a program that demonstrates a pointer to a structure. We’ll go through the program in the subsequent slides.

6 Structure Definition struct date { int month; int day; int year; };
This is the structure definition. We are setting up a structure called date. The structure has 3 data members called month, day and year. The structure definition does not allocate any memory. It is only a template for date type variables.

7 Variable Declarations
int x;   date today;   int* px;   date* dp;   px = & x;   dp = &today;   *px = 52;   (*dp).day = 21; today.month x today.day (1ab) (2fe) today.year To declare an integer variable x, we write the statement: int x; Let’s look at the general syntax, 1st you specify the data type and then you put the variable name. To declare a structure variable, you do the same thing, you put the data type in this case date and then you put the variable name today. The statement: date today; declares a structure variable called today that is of the type date. From our picture, you can see that the variable today has 3 parts: today.month, today.day and today.year. X is at address 1ab and today is at address 2fe.

8 Pointer Declarations int x;   date today;   int* px;   date* dp;   px = & x;   dp = &today;   *px = 52;   (*dp).day = 21; px dp today.month x today.day (1ab) (2fe) today.year The general syntax to declare a pointer variable is: data type, *, and variable name. So, int *px; declares px to be a pointer to an int. date * dp; declares dp to be a pointer to a structure of the type date.

9 Initializing Pointer Variables
int x;   date today;   int* px;   date* dp;   px = & x;   dp = &today;   *px = 52;   (*dp).day = 21; 1ab 2fe px dp today.month x today.day (1ab) (2fe) today.year In order to make px point to x, we write: px = &x. This means, store the address of x in px. To make dp point to the structure called today, we can write the statement: dp = &today. You can interpret this as: put the address of today in dp or make dp point to today. From our picture you can see that px contains 1ab which is the address of x. dp contains 2fe which is the address of today.

10 Indirection int x;   date today;   int* px;   date* dp;   px = & x;   dp = &today;   *px = 52;   (*dp).day = 21; 1ab 2fe px dp 52 21 today.month x today.day (1ab) (2fe) today.year The statement *px =52; is an example of indirection Interpret the statement this way: in the variable pointed to by px, store the # 52. Since px is pointing to x, the # 52 is stored in the variable x indirectly. The statement: (*dp).day = 21; indirectly stores the #21 in today.day. This is how you have to read the statement: in the variable pointed to by dp (it is pointing to today), in the day member, store the # 21.

11 Pointer to Structure Operator ->
  (*dp).day = 21; dp->day = 21; dp->month = 2; dp->year = 2040 1ab 2fe px dp 52 2 21 2040 today.month x today.day (1ab) (2fe) today.year The statement: (*dp).day = 21; indirectly stores the #21 in today.day. It’s a lot of trouble to type those parentheses and special characters. Instead of writing that statement, you can use the -> which is the pointer to a structure operator: dp->day = 21; does exactly the same operation. You can see from our picture that dp->year = 2040 stores 2040 in today.year.

12 General Rule for -> Operator
(*x). y is the same as x->y Here is the general rule for using the pointer to the structure operator. If x is a pointer to a structure and y is a data member of the same structure, writing (*x).y is the same as writing x->y. The arrow is a shortcut which can be substituted for the (* ). The arrow is formed by typing a – (hyphen) and a > sign.

13 Summary Review structures Review pointers to basic data types
Program to demonstrate pointer to structures Pointer to structure operator -> In this module, reviewed structures and pointers to basic data type. We learned about pointers to structures and the pointer to structure operator. We’ll make use of these basic building blocks in the modules to come.


Download ppt "Pointer to Structures Lesson xx"

Similar presentations


Ads by Google