1 / 8

Pointers

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

htomlinson
Télécharger la présentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers DKT121 : Fundamental of Computer Programming

  2. Outline • Introduction • Pointer Variable Definitions and Initialization • Pointer Operators DKT121 : Fundamental of Computer Programming

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. Sample program #include <stdio.h> int main() { int num; int *numPtr; int num1=5; num = 7; printf("number = %d\n", num); numPtr = &num; 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

More Related