lecture 5-pointer

# lecture 5-pointer - Pointers 1 Why Pointers They provide...

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

1 Pointers

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

View Full Document
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 .
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.

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

View Full Document
4 1003 POINTERVariable Contents Memory address 1000 1001 1002 1003 1004 1005 . . .
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;

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

View Full Document
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.
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

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

View Full Document
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 . ?
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.

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

View Full Document
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.
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; }

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

View Full Document
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.
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

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

View Full Document
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
15 4. k = &i ; i:10 j:20 k:30 5. printf (“%d”, *k ); output: 3 10 3 10

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

View Full Document
16 int a=42, b=53, *p1, *p2; p1 = &a; p2 = p1; p2 = &b; p1? p2? *p1? *p2? Example 2