Ekt120 Lecture09 Pointers
-
Upload
muhammad-faiz-bin-ahmad-shafi -
Category
Documents
-
view
221 -
download
0
Transcript of Ekt120 Lecture09 Pointers
-
8/12/2019 Ekt120 Lecture09 Pointers
1/23
UniMAP SemII-09/10 EKT 120:Computer Programming
POINTERS
-
8/12/2019 Ekt120 Lecture09 Pointers
2/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Outline Introduction
Pointer Variable Definitions and Initialization
Pointer Operators
Calling Functions by Reference
Using the const Qualifier with Pointers
Pointer Expressions and Pointer Arithmetic
Relationship between Pointers and Arrays
Arrays of Pointers
-
8/12/2019 Ekt120 Lecture09 Pointers
3/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Introduction Pointer is the address (i.e. a specific memory
location) of an object.
It can refer to different objects at different times.
Pointers are used in C programs for a variety ofpurposes: To return more than one value from a function(using pass
by reference)
To create and process strings
To manipulate the contents of arrays and structures
To construct data structures whose size can grow or shrinkdynamically
-
8/12/2019 Ekt120 Lecture09 Pointers
4/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Pointer Variable Definitions and
Initialization Pointer variables
Contain memory addresses as their values
Normal variables contain a specific value(directreference)
Pointercontains an addressof a variable that hasa specific value (indirect reference)
Indirectionreferencing a pointer value
num
7
num
7
numPtr
-
8/12/2019 Ekt120 Lecture09 Pointers
5/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Pointer Variable Definitions and
Initialization Pointer definitions
*used with pointer variables
int *numPtr; Defines a pointer to an int(pointer of type int *)
Multiple pointers require using a *before each variabledefinition
int *numPtr1, *numPtr2;
Can define pointers to any data type
Initialize pointers to 0, NULL, or an address 0or NULLpoints to nothing (NULLpreferred)
int *numPtr = NULL; orint *numPtr = 0;
-
8/12/2019 Ekt120 Lecture09 Pointers
6/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Pointer Operators Symbol &is called address operator
Returns address of operandint num = 7;
int *numPtr;numPtr = # /* numPtr gets address of num */
numPtr points to num
numPtr
num
7
numPtr
500000 600000
num
600000 7
Address of
numis value
of numPtr
-
8/12/2019 Ekt120 Lecture09 Pointers
7/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Pointer Operators Symbol *is called indirection/dereferencing
operator Returns a synonym/alias of what its operand points to
*numPtrreturns num(because numPtrpoints tonum)
*can also be used for assignment
Returns alias to an object*numPtr = 10; /* changes num to 10 */ show pictures!!
Dereferenced pointer (operand of *) must be an lvalue (no
constants)
*and are inverses They cancel each other out
-
8/12/2019 Ekt120 Lecture09 Pointers
8/23
UniMAP SemII-09/10 EKT 120:Computer Programming
#include int main(){ int num;
int *numPtr;int num1=5;
num = 7;printf("number = %d\n", num);numPtr = #printf("numPtr points to num whereby the value is = %d\n",*numPtr);printf("Address of numPtr : %d Contents of numPtr : %d\n", &numPtr, numPtr);printf("Address of num : %d\n\n", &num);
*numPtr = 15;
printf("Dereferencing pointer, *numPtr = %d\n", *numPtr);num = num + num1;printf(num = %d\n, num);printf("*numPtr = %d\n", *numPtr);printf("*numPtr + num1 = %d\n", *numPtr + num1);return 0;
}
Sample programnumber = 7numPtr points to num whereby the value is = 7Address of numPtr : 1245060 Contents of numPtr : 1245064Address of num : 1245064
Dereferencing pointer, *numPtr = 15num = 20
*numPtr = 20*numPtr + num1 = 25
-
8/12/2019 Ekt120 Lecture09 Pointers
9/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Calling Functions by Reference Call by reference with pointer arguments
Passes address of argument using &operator Allows you to change actual location in memory
Arrays are not passed with because the array name isalready a pointer
*operator Used as alias or nickname for variable inside of function
void fun1 (int *number)
{*number = 2 *(*number);}
*numberused as nickname for the variable passed
-
8/12/2019 Ekt120 Lecture09 Pointers
10/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Remember..last time#include #include char read();void find_count_vc(char, int*,int*);void print(int,int);int main()
{ char ch, choice; int count_v=0,count_c=0;do{ ch = read();
find_count_vc(ch, &count_v,&count_c);printf("Do you want to continue?");scanf("%c", &choice);getchar();
}while((choice == 'y') ||(choice =='Y'));print(count_v,count_c);
return 0;}char read(){ char ch1;
printf("Enter character : ");scanf("%c", &ch1);getchar();return(ch1);
}
void find_count_vc(char ch1, int *vowel,int *consonant){
switch(ch1){ case 'A':
case 'a':case 'E':
case 'e':case 'I':case 'i':case 'O':case 'o':case 'U':case 'u':*vowel = *vowel +1;break;default:*consonant = *consonant + 1;
}
}void print(int vowel, int consonant){
printf("Number of vowel : %d\n", vowel);printf("Number of consonant : %d\n", consonant);
}
Enter character : f
Do you want to continue?y
Enter character : I
Do you want to continue?y
Enter character : k
Do you want to continue?n
Number of vowel : 1
Number of consonant : 2
Functions that returnmore than one value i.e.
arguments are passed byref
-
8/12/2019 Ekt120 Lecture09 Pointers
11/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Rememberlast time#include const int arraySize = 10;
void initializeArray (int x [], int sizeX);void fillArray (int x [], int sizeX);void printArray (const int x [], int sizeX);
int sumArray (const int x [], int sizeX);int indexLargestElement (const int x [], int
sizeX);void copyArray (const int x [], int y [], int
length);
int main(){
int listA [arraySize] = {0};int listB [arraySize];
printArray (listA, arraySize);
initializeArray (listB, arraySize);printArray (listB, arraySize);fillArray (listA, arraySize);printArray (listA, arraySize);sumArray (listA, arraySize);copyArray (listA, listB, arraySize);printArray (listB, arraySize);return 0;
}
void initializeArray (int x [ ], int sizeX){
int counter;
for (counter = 0; counter < sizeX; counter ++)x [counter] = 0;
}
-
8/12/2019 Ekt120 Lecture09 Pointers
12/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Using the constQualifier
with Pointers constqualifier
Variable cannot be changed Use constif function does not need to change a variable Attempting to change a constvariable produces an error
constpointers Point to a constant memory location Must be initialized when defined int *const myPtr = &x;
Type int *constconstant pointer to an int const int *myPtr = &x;
Regular pointer to a const int const int *const Ptr = &x;
constpointer to a const int xcan be changed, but not *Ptr
-
8/12/2019 Ekt120 Lecture09 Pointers
13/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Pointer Expressions and
Pointer Arithmetic Arithmetic operations can be performed on
pointers
Increment/decrement pointer (++or --) Add an integer to a pointer( +or +=, -or -=)
Pointers may be subtracted from each other
Operations meaningless unless performed on an
array
-
8/12/2019 Ekt120 Lecture09 Pointers
14/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Pointer Expressions and
Pointer Arithmetic 5 element intarray on machine with 4 byte ints
vPtrpoints to first element v[ 0 ]
at location 3000 (vPtr = 3000)
vPtr += 2;sets vPtrto 3008
vPtrpoints to v[ 2 ](incremented by 2), but themachine has 4 byte ints, so it points to address 3008
pointer variable
vPtr
v[0] v[1] v[2] v[4]v[3]
3004 3008 3012 3016
location
3000
-
8/12/2019 Ekt120 Lecture09 Pointers
15/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Pointer Expressions and
Pointer Arithmetic Subtracting pointers
Returns number of elements from one to theother. If
vPtr2 = &v[ 2 ];vPtr = &v[ 0 ];
vPtr2 - vPtrwould produce 2
Pointer comparison ( ) See which pointer points to the higher numbered
array element Also, see if a pointer points to 0
-
8/12/2019 Ekt120 Lecture09 Pointers
16/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Pointer Expressions andPointer Arithmetic
Pointers of the same type can be assigned toeach other
If not the same type, a cast operator must beused
Exception: pointer to void(type void *)
Generic pointer, represents any type
No casting needed to convert a pointer to voidpointer voidpointers cannot be dereferenced
-
8/12/2019 Ekt120 Lecture09 Pointers
17/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Example of Pointer Operation#include int main(){int *vPtr; int *vPtr2;int v[5] = {10,20,30,40,50}; int temp;int *p, *q;
vPtr= v;
printf("Address of vPtr : %d Contents of vPtr: %d\n", &vPtr, vPtr);
printf("Address of v[0] : %d\n", &v);vPtr +=2;printf("Address of vPtr + 2: %d\n", vPtr);vPtr +=2;printf("Address of vPtr + 4: %d\n", vPtr);
vPtr2=&v[2];vPtr=&v[0];temp=vPtr2-vPtr;printf("Contents of temp : %d\n", temp);
p=q;printf("Contents of p : %d q: %d\n", p,q);
return 0;}
Address of vPtr : 1245064 Contents of vPtr : 1245020Address of v[0] : 1245020Address of vPtr + 2: 1245028Address of vPtr + 4: 1245036Contents of temp : 2Contents of p : 2147323904 q: 2147323904
-
8/12/2019 Ekt120 Lecture09 Pointers
18/23
UniMAP SemII-09/10 EKT 120:Computer Programming
The Relationship betweenPointers and Arrays
Arrays and pointers are closely related Array name like a constant pointer Pointers can do array subscripting operations
Define an array b[5]and a pointer bPtr To set them equal to one another use:
bPtr = b;
The array name (b) is actually the address of firstelement of the array b[ 5 ]
bPtr = b[0];
Explicitly assignsbPtrto the address of first element ofb
-
8/12/2019 Ekt120 Lecture09 Pointers
19/23
UniMAP SemII-09/10 EKT 120:Computer Programming
The Relationship betweenPointers and Arrays
Element b[3]
Can be accessed by *(bPtr + 3) Where *is the offset. Called pointer/offset notation
Can be accessed by bPtr[3] Called pointer/subscript notation
bPtr[3]same as b[3]
Can be accessed by performing pointer
arithmetic on the array itself*(b + 3)
-
8/12/2019 Ekt120 Lecture09 Pointers
20/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Example
Address of bPtr : 1245064 Contents of bPtr : 1245016Address of b : 1245016 Contents of b[0]:10 10 10bPtr points to b[0] = 10
I am accessing element b[3]!!Let see how many ways I can do itb[3] = 40*(bPtr + 3) = 40*(b + 3) = 40
bPtr[3] = 40
b[0] = 10b[1] = 20b[2] = 30b[3] = 40b[4] = 50b[5] = 0b[6] = 0b[7] = 0b[8] = 0b[9] = 0
#include int main()
{ int *bPtr ;int i;int b[10]={10,20,30,40,50};bPtr = b;printf("Address of bPtr : %d Contents of bPtr : %d\n", &bPtr, bPtr);printf("Address of b : %d Contents of b[0]:%d %d %d\n", &b, b[0], *bPtr, *b);printf("bPtr points to b[0] = %d\n", *bPtr);
printf("\nI am accessing element b[3]!!\nLet see how many ways I can do it\n");
printf("b[3] = %d\n", b[3]);printf("*(bPtr + 3) = %d\n", *(bPtr + 3));printf("*(b + 3) = %d\n", *(b + 3));printf("bPtr[3] = %d\n\n", bPtr[3]);
for(i=0;i
-
8/12/2019 Ekt120 Lecture09 Pointers
21/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Arrays of Pointers
Arrays can contain pointers
For example: an array of strings
char suit[4] = {Hearts,Diamonds,Clubs,Spades};
Strings are pointers to the first character
char *each element of suitis a pointer to a char
The strings are not actually stored in the array suit, only
pointers to the strings are stored
-
8/12/2019 Ekt120 Lecture09 Pointers
22/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Arrays of Pointers
suitarray has a fixed size, but strings can
be of any size
suit[3]
suit[2]
suit[1]
suit[0] H e a r t s \0
D i a m o n d s \0
C l u b s \0
S p a d e s \0
-
8/12/2019 Ekt120 Lecture09 Pointers
23/23
UniMAP SemII-09/10 EKT 120:Computer Programming
Example
#include #define N 5int main(){
char *studentName[N]; int i;
for(i=0;i