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