pointers

20
Pointers Sarith Divakar M [email protected] www.sarithdivakar.info 5/31/2014

Upload: sarith-divakar

Post on 22-Dec-2014

417 views

Category:

Education


3 download

DESCRIPTION

Introduction Arithmetic on a pointer Pointers and Functions Pointers and Arrays Arrays of Pointers Common Pointer Pitfalls Advanced Pointer Notation

TRANSCRIPT

Page 1: Pointers

Pointers

Sarith Divakar M [email protected]

www.sarithdivakar.info

5/31/2014

Page 2: Pointers

Agenda

• Introduction

• Arithmetic on a pointer

• Pointers and Functions

• Pointers and Arrays

• Arrays of Pointers

• Common Pointer Pitfalls

• Advanced Pointer Notation

• References

5/31/2014

Page 3: Pointers

What is a Pointer?

• A pointer is a variable which contains the address (in memory) of another variable.

• Pointers are symbolic representation of addresses

• We can have a pointer to any variable type.

5/31/2014

Page 4: Pointers

Unary Operator &

• The unary or monadic operator & gives the ``address of a variable'‘

• The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer''.

5/31/2014

Indirection Operator *

Page 5: Pointers

Declare

• We must associate a pointer to a particular type

– How many bytes of data is stored in?

• When we increment a pointer we increase the pointer by one ``block'' memory

5/31/2014

Page 6: Pointers

Example

5/31/2014

Page 7: Pointers

When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it

5/31/2014

Page 8: Pointers

Arithmetic on a pointer

5/31/2014

Page 9: Pointers

float variable (fl) and a pointer to a float (flp)

5/31/2014

Page 10: Pointers

Pointers and Functions

• When C passes arguments to functions it passes them by value.

• Let us try and write a function to swap variables around?

• The usual function call:

• swap(a, b) WON'T WORK.

• Pointers provide the solution: Pass the address of the variables to the functions

• swap(&a, &b)

5/31/2014

Page 11: Pointers

Function definition

5/31/2014

Page 12: Pointers

Pointers and Arrays

5/31/2014

• pa = a; instead of pa = &a[0] • a[i] can be written as *(pa + i).

Page 13: Pointers

How arrays are passed to functions

• When an array is passed to a function what is actually passed is its initial elements location in memory

• strlen(s) strlen(&s[0])

• This is why we declare the function:

• int strlen(char s[]);

• An equivalent declaration is : int strlen(char *s);

5/31/2014

Page 14: Pointers

Arrays of Pointers • We can have arrays of pointers since pointers

are variables.

5/31/2014

Page 15: Pointers

Common Pointer Pitfalls

• Not assigning a pointer to memory address before using it (NO COMPILER ERROR.)

• Illegal indirection – char *malloc()

– char *p;

– *p = (char *) malloc(100); /* request 100 bytes of memory */

– *p = `y';

• Malloc returns a pointer. Also p does not point to any address.

5/31/2014

Page 16: Pointers

Solution

• p = (char *) malloc(100);

• if no memory is available and p is NULL.

• Therefore we can't do:

• *p = `y';.

• We need to check it,

• if ( p == NULL) { printf(“Error”);} else {*p = `y';}

5/31/2014

Page 17: Pointers

Advanced Pointer Notation

• Two-dimensional numeric arrays

• int nums[2][3] = {{16,18,20},{25,26,27}};

5/31/2014

Pointer Notation Array Notation Value

*(*nums) nums[ 0 ] [ 0 ] 16

*(*nums + 1) nums [ 0 ] [ 1 ] 18

*(*nums + 2) nums [ 0 ] [ 2 ] 20

*(*(nums + 1)) nums [ 1 ] [ 0 ] 25

*(*(nums + 1) +1) nums [ 1 ] [ 1 ] 26

*(*(nums + 1) +2) nums [ 1 ] [ 2 ] 27

Page 18: Pointers

References

1. Cardiff School of Computer Science & Informatics http://www.cs.cf.ac.uk/Dave/C/node10.html

2. The Basics of C Programming http://computer.howstuffworks.com/c31.htm

5/31/2014

Page 19: Pointers

Questions

5/31/2014

Page 20: Pointers

Thank You

5/31/2014