POINTERS
Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference
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 of purposes: 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
Pointer Variable Definitions and Initialization Pointer variables 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 num 7 num 7 numPtr
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 variable definition int *numPtr1, *numPtr2; Can define pointers to any data type Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing (NULL preferred) int *numPtr = NULL; OR int *numPtr = 0;
Pointer Operators Symbol & is called address operator Returns address of operand int num = 7; int *numPtr; numPtr = # /* numPtr gets address of num */ numPtr “points to” num numPtr num 7 500000 600000 Address of num is value of numPtr
Pointer Operators Symbol * is called indirection/dereferencing operator Returns a synonym/alias of what its operand points to *numPtr returns num (because numPtr points to num) * 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
Sample program *numPtr = 15; number = 7 numPtr points to num whereby the value is = 7 Address of numPtr : 1245060 Contents of numPtr : 1245064 Address of num : 1245064 Dereferencing pointer, *numPtr = 15 num = 20 *numPtr = 20 *numPtr + num1 = 25 #include <stdio.h> 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; }
Calling Functions by Reference Call by reference with pointer arguments Pass 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 Used as alias/nickname for variable inside of function void fun1( int *number ) { *number = 2 * ( *number ); } *number used as nickname for the variable passed
Enter character : f Do you want to continue?y Enter character : I Enter character : k Do you want to continue?n Number of vowel : 1 Number of consonant : 2 Remember..last time #include <stdio.h> #include <string.h> 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); 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); Functions that “return” more than one value i.e. arguments are passed by ref