80 likes | 97 Views
Pointers. Outline. Introduction Pointer Variable Definitions and Initialization Pointer Operators. Introduction. Pointer is the address(i.e. a specific memory location) of an object It can refer to different objects at different times
E N D
Pointers DKT121 : Fundamental of Computer Programming
Outline • Introduction • Pointer Variable Definitions and Initialization • Pointer Operators DKT121 : Fundamental of 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 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 • To construct data structures whose size can grow or shrink dynamically DKT121 : Fundamental of Computer Programming
num numPtr num 7 7 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 DKT121 : Fundamental of 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 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; DKT121 : Fundamental of Computer Programming
numPtr num num 7 numPtr Address of num is value of numPtr 500000 600000 600000 7 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 DKT121 : Fundamental of Computer Programming
Pointer Operators • Symbol * is called indirection/dereferencing operator • Returns a synonym/alias of what its operand points to • *numPtr returns num (because numPtr points 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 DKT121 : Fundamental of Computer Programming
Sample program #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; } 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 DKT121 : Fundamental of Computer Programming