Pointers - Pointers in C Call By Reference 1 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard

Pointers - Pointers in C Call By Reference 1 Pre-requisite...

This preview shows page 1 - 14 out of 41 pages.

1 Pointers in C Call By Reference
Image of page 1
2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
Image of page 2
3 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary
Image of page 3
4 Computer Memory Revisited Computers store data in memory slots Each slot has an unique address Variables store their values like this: Addr Content Addr Content Addr Content Addr Content 1000 i: 37 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘c’ 1007 a[3]: ‘\0’ 1008 ptr: 1001 1009 1010 1011
Image of page 4
5 Computer Memory Revisited Altering the value of a variable is indeed changing the content of the memory e.g. i = 40; a[2] = ‘z’; Addr Content Addr Content Addr Content Addr Content 1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘\0’ 1008 ptr: 1001 1009 1010 1011
Image of page 5
6 Addressing Concept Pointer stores the address of another entity It refers to a memory location int i = 5; int * ptr; /* declare a pointer variable */ ptr = & i; /* store address-of i to ptr */ printf(“*ptr = %d\n”, *ptr ); /* refer to referee of ptr */
Image of page 6
7
Image of page 7
8 What actually ptris?
Image of page 8
9 Twin Operators &: Address-of operator Get the address of an entity e.g. ptr = &j; Addr Content Addr Content Addr Content Addr Content 1000 i: 40 1001 j: 33 1002 k: 58 1003 m: 74 1004 ptr: 1001 1005 1006 1007
Image of page 9
10 Twin Operators *: De-reference operator Refer to the content of the referee e.g. *ptr = 99; Addr Content Addr Content Addr Content Addr Content 1000 i: 40 1001 j: 99 1002 k: 58 1003 m: 74 1004 ptr: 1001 1005 1006 1007
Image of page 10