ch6 pointers (latest)
DESCRIPTION
TRANSCRIPT
1
Chapter 6 – POINTERS
• Pointer• Pointers Declaration & Initialization• Pointer Operators• Calling Function using Pointers• Array of Pointers
• Array of string
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
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
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
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)
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;
7
6.1 2 Pointer Operators
8
6.1.2 Pointer Operators (Cont..)
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
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);
11
Example 2
12
Example (Cont..)
Refer to Add Note : Ex. 1 & 2
13
6.1.3 Calling Function using Pointers
Refer to Add Note : Ex. 3
14
6.1.4 Arrays of Pointers
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));
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. }
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.
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);