Pointers
-
Upload
sarith-divakar -
Category
Education
-
view
416 -
download
3
description
Transcript of 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
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
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 *
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
Example
5/31/2014
When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it
5/31/2014
Arithmetic on a pointer
5/31/2014
float variable (fl) and a pointer to a float (flp)
5/31/2014
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
Function definition
5/31/2014
Pointers and Arrays
5/31/2014
• pa = a; instead of pa = &a[0] • a[i] can be written as *(pa + i).
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
Arrays of Pointers • We can have arrays of pointers since pointers
are variables.
5/31/2014
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
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
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
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
Questions
5/31/2014
Thank You
5/31/2014