# POINTERS Uni MAP Sem I1112 EKT 120 Computer

• Slides: 23

POINTERS Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Outline n n n n 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 of Pointers Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Introduction n 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 of purposes: n n 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 shrink dynamically Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Pointer Variable Definitions and Initialization n Pointer variables n n Contain memory addresses as their values Normal variables contain a specific value (direct reference) i. Num 7 n n Pointer contains an address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value pi. Num Uni. MAP Sem. I-11/12 i. Num 7 EKT 120: Computer Programming

Pointer Variable Definitions and Initialization n Pointer definitions n n n * used with pointer variables int *pi. Num; Defines a pointer to an int (pointer of type int *) Multiple pointers require using a * before each variable definition int *pi. Num 1, *pi. Num 2; Can define pointers to any data type Initialize pointers to 0, NULL, or an address n 0 or NULL – points to nothing (NULL preferred) n int *pi. Num = NULL; or int *pi. Num = 0; Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Pointer Operators n Symbol & is called address operator n Returns address of operand int i. Num = 7; int *pi. Num; pi. Num = &i. Num; /* pi. Num gets address of i. Num */ pi. Num “points to” i. Num pi. Num 7 pi. Num 500000 600000 i. Num 600000 Address of i. Num is value of pi. Num Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 7

Pointer Operators n Symbol * is called indirection/dereferencing operator n Returns a synonym/alias of what its operand points to n n *pi. Num returns i. Num (because pi. Num points to i. Num) * can also be used for assignment n Returns alias to an object *pi. Num = 10; n n /* changes i. Num to 10 */ Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are inverses n show pictures!! They cancel each other out Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Sample program #include <stdio. h> int main() { int i. Num; int *pi. Num; int i. Num 1=5; number = 7 pi. Num points to i. Num whereby the value is = 7 Address of pi. Num : 1245060 Contents of pi. Num : 1245064 Address of i. Num : 1245064 Dereferencing pointer, *pi. Num = 15 i. Num = 20 *pi. Num + i. Num 1 = 25 i. Num = 7; printf("number = %dn", i. Num); pi. Num = &i. Num; printf(“pi. Num points to i. Num whereby the value is = %dn", *pi. Num); printf("Address of pi. Num : %d Contents of pi. Num : %dn", &pi. Num, pi. Num); printf("Address of i. Num : %dnn", &i. Num); } *pi. Num = 15; printf("Dereferencing pointer, *pi. Num = %dn", *pi. Num); i. Num = i. Num + i. Num 1; printf(“i. Num = %dn”, i. Num); printf("*pi. Num = %dn", *pi. Num); printf("*pi. Num + i. Num 1 = %dn", *pi. Num + i. Num 1); return 0; Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Calling Functions by Reference n Call by reference with pointer arguments n n Passes address of argument using & operator Allows you to change actual location in memory Arrays are not passed with ‘&’ because the array name is already a pointer * operator n Used as alias or nickname for variable inside of function n void fn. Fun 1 (int *pi. Number) { *pi. Number = 2 * (*pi. Number); } *pi. Number used as nickname for the variable passed Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Remember. . last time #include <stdio. h> #include <string. h> char fn. Read(); void fn. Find. Count. VC(char, int*); void fn. Print(int, int); 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 void fn. Find. Count. VC(char c. Ch 1, int *pi. Vowel, int *pi. Consonant) int main() { { char c. Ch, c. Choice; int i. Count. V=0, i. Count. C=0; switch(c. Ch 1) do Functions that “return” { case 'A': { c. Ch = fn. Read(); more than one value i. e. case 'a': fn. Find. Count. VC(c. Ch, &i. Count. V, &i. Count. C); arguments are passed by case 'E': printf("Do you want to continue? "); reference case 'e': scanf("%c", &c. Choice); case 'I': getchar(); case 'i': }while((c. Choice == 'y') ||(c. Choice =='Y')); case 'O': fn. Print(i. Count. V, i. Count. C); case 'o': return 0; case 'U': } case 'u': *pi. Vowel = *pi. Vowel +1; break; default: *pi. Consonant = *pi. Consonant + 1; char fn. Read() } { char c. Ch 1; } printf("Enter character : "); void fn. Print(int i. Vowel, int i. Consonant) scanf("%c", &c. Ch 1); { getchar(); printf("Number of vowel : %dn", i. Vowel); return(c. Ch 1); printf("Number of consonant : %dn", i. Consonant); } 10 }

Remember…last time #include <stdio. h> const int i. Array. Size = 10; void fn. Initialize. Array (int ai. X[], int i. Size. X); void fn. Fill. Array (int ai. X[], int i. Size. X); void fn. Print. Array (const int ai. X[], int i. Size. X); int fn. Sum. Array (const int ai. X[], int i. Size. X); int fn. Index. Largest. Element (const int ai. X[], int i. Size. X); void fn. Copy. Array (const int ai. X[], int ai. Y[], int i. Length); void fn. Initialize. Array (int ai. X[ ], int i. Size. X) { int i. Counter; int main() { int ai. List. A [i. Array. Size] = {0}; int ai. List. B [i. Array. Size]; } fn. Print. Array (ai. List. A, i. Array. Size); fn. Initialize. Array (ai. List. B, i. Array. Size); fn. Print. Array (ai. List. B, i. Array. Size); fn. Fill. Array (ai. List. A, i. Array. Size); fn. Print. Array (ai. List. A, i. Array. Size); fn. Sum. Array (ai. List. A, i. Array. Size); fn. Copy. Array (ai. List. A, ai. List. B, i. Array. Size); fn. Print. Array (ai. List. B, i. Array. Size); return 0; for (i. Counter = 0; i. Counter < i. Size. X; i. Counter++) ai. X[i. Counter] = 0; } Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Using the const Qualifier with Pointers n n const qualifier n Variable cannot be changed n Use const if function does not need to change a variable n Attempting to change a const variable produces an error const pointers n Point to a constant memory location n Must be initialized when defined n int *const pi. My. Ptr = &i. X; n Type int *const – constant pointer to an int n const int *pi. My. Ptr = &i. X; n Regular pointer to a const int n const int *const pi. Ptr = &i. X; n const pointer to a const int n i. X can be changed, but not *pi. Ptr Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Pointer Expressions and Pointer Arithmetic n Arithmetic operations can be performed on pointers n n 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 Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Pointer Expressions and Pointer Arithmetic n 5 element int array on machine with 4 byte ints n n pi. VPtr points to first element ai. V[ 0 ] n at location 3000 (pi. VPtr = 3000) pi. VPtr += 2; sets pi. VPtr to 3008 n pi. VPtr points to ai. V[ 2 ] (incremented by 2), but the machine has 4 byte ints, so it points to address 3008 location 3000 3004 ai. V[0] pointer variable pi. VPtr Uni. MAP Sem. I-11/12 ai. V[1] 3008 ai. V[2] 3012 3016 ai. V[3] EKT 120: Computer Programming ai. V[4]

Pointer Expressions and Pointer Arithmetic n Subtracting pointers n Returns number of elements from one to the other. If pi. VPtr 2 pi. VPtr n n = &ai. V[ 2 ]; = &ai. V[ 0 ]; pi. VPtr 2 - pi. VPtr would produce 2 Pointer comparison ( <, == , > ) n n See which pointer points to the higher numbered array element Also, see if a pointer points to 0 Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Pointer Expressions and Pointer Arithmetic n Pointers of the same type can be assigned to each other n n If not the same type, a cast operator must be used Exception: pointer to void (type void *) n n n Generic pointer, represents any type No casting needed to convert a pointer to void pointers cannot be dereferenced Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Example of Pointer Operation #include <stdio. h> int main() {int *pi. VPtr; int *pi. VPtr 2; int ai. V[5] = {10, 20, 30, 40, 50}; int i. Temp; int *pi. P, *pi. Q; pi. VPtr= ai. V; printf("Address of pi. VPtr : %d Contents of pi. VPtr : %dn", &pi. VPtr, pi. VPtr); printf("Address of ai. V[0] : %dn", &ai. V); pi. VPtr +=2; printf("Address of pi. VPtr + 2: %dn", pi. VPtr); pi. VPtr +=2; printf("Address of pi. VPtr + 4: %dn", pi. VPtr); Address of pi. VPtr : 1245064 Contents of pi. VPtr : 1245020 Address of ai. V[0] : 1245020 Address of pi. VPtr + 2: 1245028 Address of pi. VPtr + 4: 1245036 Contents of temp : 2 Contents of pi. P : 2147323904 pi. Q : 2147323904 pi. VPtr 2=&ai. V[2]; pi. VPtr=&ai. V[0]; i. Temp=pi. VPtr 2 -pi. VPtr; printf("Contents of i. Temp : %dn", i. Temp); pi. P=pi. Q; printf("Contents of pi. P : %d pi. Q : %dn", pi. P, pi. Q); return 0; } Uni. MAP Sem. II-09/10 EKT 120: Computer Programming

The Relationship between Pointers and Arrays n Arrays and pointers are closely related n n n Array name like a constant pointer Pointers can do array subscripting operations Define an array ai. B[5] and a pointer pi. BPtr n To set them equal to one another use: n n pi. BPtr = ai. B; The array name (ai. B) is actually the address of first element of the array ai. B[ 5 ] pi. BPtr = &ai. B[0]; Explicitly assigns pi. BPtr to the address of first element of ai. B Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

The Relationship between Pointers and Arrays n Element ai. B[3] n Can be accessed by *(pi. BPtr + 3) n n Can be accessed by pi. BPtr[3] n n n Where * is the offset. Called pointer/offset notation Called pointer/subscript notation pi. BPtr[3] same as ai. B[3] Can be accessed by performing pointer arithmetic on the array itself *(ai. B + 3) Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Address of pi. BPtr : 1245064 Contents of pi. BPtr : 1245016 Address of ai. B : 1245016 Contents of ai. B[0]: 10 10 10 pi. BPtr points to ai. B[0] = 10 Example I am accessing element ai. B[3]!! Let see how many ways I can do it ai. B[3] = 40 *(pi. BPtr + 3) = 40 *(ai. B + 3) = 40 pi. BPtr[3] = 40 ai. B[0] ai. B[1] ai. B[2] ai. B[3] ai. B[4] ai. B[5] ai. B[6] ai. B[7] ai. B[8] ai. B[9] = = = = = 10 20 30 40 50 0 0 #include <stdio. h> int main() { int *pi. BPtr ; int i. Index; int ai. B[10]={10, 20, 30, 40, 50}; pi. BPtr = ai. B; printf("Address of pi. BPtr : %d Contents of pi. BPtr : %dn", &pi. BPtr, pi. BPtr); printf("Address of ai. B : %d Contents of ai. B[0]: %d %d %dn", &ai. B, ai. B[0], *pi. BPtr, *ai. B); printf(“pi. BPtr points to ai. B[0] = %dn", *pi. BPtr); printf("n. I am accessing element ai. B[3]!!n. Let see how many ways I can do itn"); printf(“ai. B[3] = %dn", ai. B[3]); printf("*(pi. BPtr + 3) = %dn", *(pi. BPtr + 3)); printf("*(ai. B + 3) = %dn", *(ai. B + 3)); printf(“pi. BPtr[3] = %dnn", pi. BPtr[3]); for(i. Index=0; i. Index<10; i. Index++) printf(“ai. B[%d] = %dn", i. Index, *(pi. BPtr+i. Index)); return 0; Uni. MAP Sem. I-11/12 EKT 120: Computer Programming }

Arrays of Pointers n n Arrays can contain pointers For example: an array of strings char *ac. Suit[4] = {“Hearts”, “Diamonds”, “Clubs”, “Spades”}; n n n Strings are pointers to the first character char * – each element of ac. Suit is a pointer to a char The strings are not actually stored in the array ac. Suit, only pointers to the strings are stored Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Arrays of Pointers ac. Suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’’ ac. Suit[1] ’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ac. Suit[2] ’C’ ’l’ ’u’ ’b’ ’s’ ’’ ac. Suit[3] ’S’ ’p’ ’a’ ’d’ ’e’ ’s’ n ’s’ ’’ ac. Suit array has a fixed size, but strings can be of any size Uni. MAP Sem. I-11/12 EKT 120: Computer Programming

Example Enter student[0] name You just entered : ali Enter student[1] name You just entered : abu Enter student[2] name for(i. Index=0; i. Index<5; i. Index++) You just entered : { printf("Enter student[%d] name : ", i. Index); cheah scanf("%s", ac. Student. Name + i. Index); printf("You just entered : n%sn", ac. Student. Name + i. Index); Enter student[3] name You just entered : } dali return 0; Enter student[4] name } You just entered : gheeta #include <stdio. h> #define N 5 int main() { char *ac. Student. Name[N]; int i. Index; Uni. MAP Sem. I-11/12 EKT 120: Computer Programming : ali : abu : cheah : dali : gheeta