Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer.

Similar presentations


Presentation on theme: "Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer."— Presentation transcript:

1 Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx
 In this presentation, we will illustrate the difference between a pointer to structure and a structure that contains a pointer.

2 Objectives Illustrate the difference between a pointer to structure and a structure that contains a pointer When to use the pointer to structure (-> ) operator When to use the dot (.) operator Our goal is to illustrate the difference between a pointer to structure and a structure that contains a pointer. We will also show you when you the -> and the . operator which is a source of much confusion to students learning about pointers.

3 Structure Setup struct student { long id; int age; }; int main ( )
student s; s.id = 25691; s.age= 17; student *ps = &s; return 0; } 25691 17 s.id s.age (2fe) s We have defined a structure called student so that we can keep track of a student ids and ages. In main() we set up a student object called s which has two parts: s.id and s.age. The id is initialized to and the age is 17.

4 Pointer to a Structure struct student { long id; int age; };
int main ( ) student s; s.id = 25691; s.age= 17; student *ps = &s; return 0; } 25691 17 s.id s.age (2fe) 2fe ps s In the last part of main(), we have defined a pointer to a structure called ps. At the same time, we made ps point to the structure s. You can see from our picture that ps contains 2fe which is the address of s. This is an example of a pointer to a structure. The pointer is ps and the structure is s.

5 Legal Expressions with a Pointer to a Structure
s.id s.age ps-> id ps->age 25691 17 s.id s.age (2fe) 2fe ps s Let’s talk about expressions that are legal when you have the drawing that is shown. s is a structure variable and structure variables have data members. The parts of s are s.id and s.age. With structues, you use the .(dot) operator because that is the member operator. ps->id refers to the structure pointed to by ps which in this case is s. Since s is a structure, you have to specify which part of the structure you are refering to, in this case id. To sum it up for you, use a . when you have a structure variable. Use an arrow when you have a pointer to a structure.

6 Illegal Expressions with a Pointer to a Structure
s->id s->age ps. id ps.age 25691 17 s.id s.age (2fe) 2fe ps s Sometimes it’s easier to look at illegal expressions for comparison purposes. s->id and s->age are illegal because s is structure and not a pointer to a structure. With structure variables, you use the . which is the member operator. ps.id and ps.age are illegal because ps is a pointer to a structure. With pointer to structures, you use the -> which is called the pointer to structure operator

7 Different Syntax with Pointer to a Structure
s.id (*ps).id ps->id 25691 17 s.id s.age (2fe) 2fe ps s s.id, (*ps).id and ps->id all refer to the same memory location that contains s.id refers to the location directly. (*ps).id refers to the location indirectly using the pointer ps and ps->id is the shortcut for (*ps).id. Later on, we’ll see why we need indirection.

8 Incorrect Syntax with Pointer to a Structure
//illegal syntax *ps.id *(ps.id) 25691 17 s.id s.age (2fe) 2fe ps s *ps.id and *(ps.id) are both illegal statements when ps is a pointer to a structure. It is not the same as (*ps).id. They look similar but, *ps.id will not compile because the computer interprets the statement as * (ps.id). Since ps is not a structure, it cannot have a member called id. ps is a pointer to a structure. //legal syntax (*ps).id

9 Structure Containing Pointers
  struct student { long *id; int *age; }; int main ( ) student s; long i; int a; s.id = &i; s.age= &a; *s.id = = 25691; *s.age = 17; return 0; } s (55d) s.id s.age We have defined a structure here called student and it contains 2 data members called id and age which are pointers. In the first line of main(), student s; declares a student object called s. This is a silly structure but we are going to use it for syntax comparison purposes later.

10 Variable Declarations
  struct student { long *id; int *age; }; int main ( ) student s; long i; int a; s.id = &i; s.age= &a; *s.id = = 25691; *s.age = 17; return 0; } s (55d) a i (1ab) (2fe) s.id s.age long i; and int a are variable declarations that set up 2 memory locations called i and a. In the picture, i is at address 1ab, a is at 2f3 and s is at 55d.

11 Initializing a Structure That Contains Pointers
  struct student { long *id; int *age; }; int main ( ) student s; long i; int a; s.id = &i; s.age= &a; *s.id = = 25691; *s.age = 17; return 0; } s (55d) a i (1ab) (2fe) 1ab 2fe s.id s.age s.id = &i; and s.age = &a; initializes the pointers contained in the structure s. s.id contains the address of I and s.age contains the address of a. Another way of saying this is that s.id points to i and s.age points to a.

12 Indirection with Structure Containing Pointers
  struct student { long *id; int *age; }; int main ( ) student s; long i; int a; s.id = &i; s.age= &a; *s.id = ; *s.age = 17; return 0; } s (55d) 25691 17 a i (1ab) (2fe) 1ab 2fe s.id s.age *s.id = 25691; and *s.age = 17 are examples of indirection when you have structures that contain pointers. *s.age = 17; means: in the variable pointed to by s.age (it’s pointing to a), store the # 17. You can see in the picture that the variable a contains the # 17.

13 Legal Expressions with Structure That Contains Pointers
*s.id = 25691; *(s.id)= 25691; (*s.id) = 25691; *s.age = 17; s (55d) 25691 17 a i (1ab) (2fe) 1ab 2fe s.id s.age The 4 statements listed are legal expressions when you have a structure that contains a pointer. With the statement *s.id =25691; the computer interprets this as: *(s.id) = 25691; because the . operator has more precedence than the * operator. (*s.id) =25691 is equivalent to the 1st 2 statements. Here the ( )s are redundant but still legal.

14 Illegal Expressions with a Structure That Contains Pointers
(55d) (*s).id = 25691; s->id= 25691; 25691 17 a i (1ab) (2fe) 1ab 2fe s.id s.age (*s).id = 25691; is illegal. *s means: in the variable pointed to by s. s is a structure and not a pointer to a structure so there is no such expression such as *s. s->id is also illegal because -> is the pointer to the structure operator. Since s is not a pointer to a structure, you cannot use the ->.

15 Comparison 25691 17 s.id s.age (2fe) 2fe ps s
ps is a pointer to the structure s. The top picture is an example of a pointer to a structure. This is when you can use the -> which is the pointer to a structure operator. The bottom picture is an example of a structure that contains a pointer. You cannot use the -> because you don’t have a pointer to a structure. 25691 17 a i (1ab) (2fe) 1ab 2fe s.id s.age s (55d) s is a structure that contains 2 pointers

16 Summary Illustrate the difference between a pointer to structure and a structure that contains a pointer When to use the pointer to structure (-> ) operator When to use the dot (.) operator In summary, we have learned about: difference between a pointer to structure and a structure that contains a pointer. We talked about when to use the pointer to structure (-> ) operator and when to use the dot (.) operator.


Download ppt "Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer."

Similar presentations


Ads by Google