pointer - Pointers 1 Why Pointers They provide the means by...

Info iconThis preview shows pages 1–22. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Pointers
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Why Pointers They provide the means by which functions can modify arguments in the calling function. They support dynamic memory allocation . They provide support for dynamic data structures, such as binary trees and linked lists .
Background image of page 2
3 What Are Pointers ? A pointer is the memory address of an object. A pointer variable is a variable that is specifically declared to hold a pointer to an object of its specified type. This address is the location of another object (typically another variable) in memory.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 1003 POINTERVariable Contents Memory address 1000 1001 1002 1003 1004 1005 . . .
Background image of page 4
5 Pointer Declaration General syntax : type * name ; int * m; //The variable m can hold a //pointer to type int. char * ch; int count, * y, q;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Two Pointer Operators Address Operator : & Dereference Operator : * Both & and * have a higher precedence than all other arithmetic operators except the unary minus, with which they share equal precedence.
Background image of page 6
7 & Operator The & is a unary operator that returns the memory address of its operand . & “the address of” m = &count ; m receives the address of count. m count 100
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 * Operator It is a unary operator that returns the value located at the address that follows. * “at address” q = *m ; q receives the value at address m . ?
Background image of page 8
9 #include <stdio.h> int main(void) { int target, source=10; int *m ; m = &source ; target = *m ; printf("%d", target); return 0; } Put the value 10 into a variable called target.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Pointer Assignments You can use a pointer variable on the right-hand side of an assignment statement to assign its value to another pointer. When both pointers are the same type, the situation is straightforward.
Background image of page 10
11 #include <stdio.h> int main(void) { int x = 99; int *p1, *p2 ; p1 = &x ; p2 = p1 ; printf(" %p %p ", p1, p2); printf(''%d %d\n", *p1, *p2 ); return 0; }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Illustrates Distinction between a pointer var value & a Dereference variable . main( ) { int i = 777, *p = &i ; printf (“value of i:% d \n”, *p ); printf (“Addr of i :%u or %p\n ”, p, p); } Output value of i: 777 Addr of i: 234880259 or dfffcfc u - (unsigned Dec integer) p - (whatever way is Default for system) - Here is Hex.
Background image of page 12
13 Example 1 int i = 1, *j, *k ; Assume addresses of i, j, k are respectively Byte addresses 10, 20, 30 i:10 j:20 k:30 1. j = &i ; int var Pointer var Pointer var i:10 j:20 k:30 10 ? ? 1 ? 1
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 2. *j = 2 ; i:10 j:20 k:30 Stores 2 at the memory location pointed to by j. 3. i = *j + 1 ; i:10 j:20 k:30 * has higher precedence than +. A ssigns to i the contents of the location pointed to by j, incremented by 1. 10 ? 1 2 10 ? 12 3
Background image of page 14
15 4. k = &i ; i:10 j:20 k:30 5. printf (“%d”, *k ); output: 3 10 3 10
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
16 int a=42, b=53, *p1, *p2; p1 = &a; p2 = p1; p2 = &b; p1? p2? *p1? *p2? Example 2
Background image of page 16