Your SlideShare is downloading. ×
0
Chapter 6 – POINTERS <ul><li>Pointer </li></ul><ul><ul><li>Pointers Declaration & Initialization </li></ul></ul><ul><ul><l...
Objectives <ul><li>To have acquired the ability to design and manipulate pointer variables. </li></ul><ul><li>To be able t...
6.1  Pointers <ul><li>A variable has a name, an address, a type and a value </li></ul><ul><li>Pointers are language constr...
6.1.1 Declaration & Initialization <ul><li>Pointer variable </li></ul><ul><li>Contain memory addresses as their values </l...
6.1.1 Declaration & Initialization(Cont..) <ul><li>Pointer declarations </li></ul><ul><li>*  used with pointer variables <...
6.1.1 Declaration & Initialization (Cont..) <ul><li>Pointer Initializations </li></ul><ul><li>2 ways :- </li></ul><ul><li>...
6.1 2 Pointer Operators
6.1.2 Pointer Operators (Cont..)
6.1.2 Pointer Operators (Cont..) <ul><li>Short Notes : </li></ul><ul><li>i) int *px;  px = pointer to an integer  </li></u...
Example 1 <ul><li>int age, *ag;  …(i) </li></ul><ul><li>age = 30;  ….(ii) </li></ul><ul><li>ag =&age;  ….(iii) </li></ul><...
Example 2
Example (Cont..) Refer to Add Note : Ex. 1 & 2
6.1.3 Calling Function using Pointers Refer to Add Note : Ex. 3
6.1.4 Arrays of Pointers
Example <ul><li>char *name[5] = { { “Ahmad”},  </li></ul><ul><li>{“Maria”},  </li></ul><ul><li>{“Halim”},  </li></ul><ul><...
Example Program <ul><li>/* Example : Arrays of pointers*/ </li></ul><ul><li>/*Increments a pointer through an integer arra...
Exercise <ul><li>Write a C statement to accomplish the following tasks. </li></ul><ul><li>Assume that the floating point v...
Exercise <ul><li>Write a C statement to accomplish the following tasks. </li></ul><ul><li>Assume that the floating point v...
Upcoming SlideShare
Loading in...5
×

Ch6 pointers (latest)

1,570

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,570
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
165
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Ch6 pointers (latest)"

  1. 1. Chapter 6 – POINTERS <ul><li>Pointer </li></ul><ul><ul><li>Pointers Declaration & Initialization </li></ul></ul><ul><ul><li>Pointer Operators </li></ul></ul><ul><ul><li>Calling Function using Pointers </li></ul></ul><ul><ul><li>Array of Pointers </li></ul></ul><ul><ul><ul><li>Array of string </li></ul></ul></ul>
  2. 2. Objectives <ul><li>To have acquired the ability to design and manipulate pointer variables. </li></ul><ul><li>To be able to design and use user-defined data types- structures </li></ul>
  3. 3. 6.1 Pointers <ul><li>A variable has a name, an address, a type and a value </li></ul><ul><li>Pointers are language constructs that allow programmers to directly manipulate the address of variables. </li></ul><ul><li>Simulate call-by-reference </li></ul><ul><li>Close relationship with arrays and strings </li></ul>
  4. 4. 6.1.1 Declaration & Initialization <ul><li>Pointer variable </li></ul><ul><li>Contain memory addresses as their values </li></ul><ul><li>Normal variables contain a specific value (direct reference) </li></ul><ul><li>Pointers contain address of a variable that has a specific value (indirect reference) </li></ul><ul><li>Indirection – referencing a pointer value </li></ul>
  5. 5. 6.1.1 Declaration & Initialization(Cont..) <ul><li>Pointer declarations </li></ul><ul><li>* used with pointer variables </li></ul><ul><li>int * myPtr; </li></ul><ul><li>Declares a pointer to an int (pointer of type int * ) </li></ul><ul><li>Multiple pointers require using a * before each variable </li></ul><ul><li>declaration </li></ul><ul><li>int * myPtr1, * myPtr2; </li></ul><ul><li>Can declare pointers to any data type </li></ul><ul><li>Initialize pointers to 0 , NULL , or an address </li></ul><ul><li>0 or NULL – points to nothing ( NULL preferred) </li></ul>
  6. 6. 6.1.1 Declaration & Initialization (Cont..) <ul><li>Pointer Initializations </li></ul><ul><li>2 ways :- </li></ul><ul><li>i) Assigning address of other variable </li></ul><ul><li>ii) assigning value of other variable directly </li></ul><ul><li>Method (i) </li></ul><ul><li> Example : int val = 30; </li></ul><ul><li> int *value =&val; </li></ul><ul><li>Method (ii) </li></ul><ul><li> Example : </li></ul><ul><li>int *value ; </li></ul><ul><li> *value = 30; </li></ul>
  7. 7. 6.1 2 Pointer Operators
  8. 8. 6.1.2 Pointer Operators (Cont..)
  9. 9. 6.1.2 Pointer Operators (Cont..) <ul><li>Short Notes : </li></ul><ul><li>i) int *px; px = pointer to an integer </li></ul><ul><li>ii) int x; x is an integer </li></ul><ul><li>iii) px = &x; px gets the address of x or px point to x </li></ul><ul><li>iv) y = *px; y gets the content of whatever </li></ul><ul><li> px points to </li></ul>
  10. 10. Example 1 <ul><li>int age, *ag; …(i) </li></ul><ul><li>age = 30; ….(ii) </li></ul><ul><li>ag =&age; ….(iii) </li></ul><ul><li>Situation in Computer Memory . </li></ul><ul><li>After (i) </li></ul><ul><li>age 1002 </li></ul><ul><li>ag 1004 </li></ul><ul><li>B) After (ii) </li></ul><ul><li>age 1002 </li></ul><ul><li>ag 1004 </li></ul>? ? 30 ? C) After (iiI) age 1002 ag 1004 30 1002 Assume that the following codes are added after (iii). Predict the output values ? printf(“%dn”, age); printf(“%dn”, ag); printf(“%dn”, *ag);
  11. 11. Example 2
  12. 12. Example (Cont..) Refer to Add Note : Ex. 1 & 2
  13. 13. 6.1.3 Calling Function using Pointers Refer to Add Note : Ex. 3
  14. 14. 6.1.4 Arrays of Pointers
  15. 15. Example <ul><li>char *name[5] = { { “Ahmad”}, </li></ul><ul><li>{“Maria”}, </li></ul><ul><li>{“Halim”}, </li></ul><ul><li>{“Nora”}, </li></ul><ul><li>{“Lina”} }; </li></ul><ul><li>To print 1 st String </li></ul><ul><li>printf(“%s”, *name); </li></ul><ul><li>To printf 2 nd String </li></ul><ul><li>printf(“%s”, (*name+1)); </li></ul>
  16. 16. Example Program <ul><li>/* Example : Arrays of pointers*/ </li></ul><ul><li>/*Increments a pointer through an integer array*/ </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>main( ) { </li></ul><ul><li>int number[] = { 10,20,30,40,50}; </li></ul><ul><li>int *p = number; /* The pointers points to the start of the array*/ </li></ul><ul><li>printf(“%d n”, *p); </li></ul><ul><li>p++; </li></ul><ul><li>printf(“%d n”, *p); </li></ul><ul><li>p++; </li></ul><ul><li>printf(“%d n”, *p); </li></ul><ul><li>p++; </li></ul><ul><li>printf(“%d n”, *p); </li></ul><ul><li>p++; </li></ul><ul><li>printf(“%d n”, *p); </li></ul><ul><li>} </li></ul>
  17. 17. Exercise <ul><li>Write a C statement to accomplish the following tasks. </li></ul><ul><li>Assume that the floating point variable number1 has been declared. </li></ul><ul><ul><li>Define the variable fPtr to be a pointer to an object of type float. </li></ul></ul><ul><ul><li>Assign the address of variable number1 to pointer variable fPtr. </li></ul></ul><ul><ul><li>Print the value of the object pointed to by fPtr. </li></ul></ul>
  18. 18. Exercise <ul><li>Write a C statement to accomplish the following tasks. </li></ul><ul><li>Assume that the floating point variable number1 has been </li></ul><ul><li>declared. </li></ul><ul><ul><li>Define the variable fPtr to be a pointer to an object of type float. </li></ul></ul><ul><ul><li>Assign the address of variable number1 to pointer variable fPtr. </li></ul></ul><ul><ul><li>Print the value of the object pointed to by fPtr. </li></ul></ul><ul><ul><li>Answers : </li></ul></ul><ul><ul><li>float *fPtr; </li></ul></ul><ul><ul><li>fptr = &number1; </li></ul></ul><ul><ul><li>printf(“The value of object pointed is %d”, *fPtr); </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×