Pointers

4,103 views

Published on

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

Published in: Education
2 Comments
8 Likes
Statistics
Notes
No Downloads
Views
Total views
4,103
On SlideShare
0
From Embeds
0
Number of Embeds
259
Actions
Shares
0
Downloads
621
Comments
2
Likes
8
Embeds 0
No embeds

No notes for slide

Pointers

  1. 1. Pointers Sarith Divakar M sarith@opisglobal.com www.sarithdivakar.info5/31/2014
  2. 2. 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
  3. 3. 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
  4. 4. 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 *
  5. 5. 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
  6. 6. Example 5/31/2014
  7. 7. When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it 5/31/2014
  8. 8. Arithmetic on a pointer 5/31/2014
  9. 9. float variable (fl) and a pointer to a float (flp) 5/31/2014
  10. 10. 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
  11. 11. Function definition 5/31/2014
  12. 12. Pointers and Arrays 5/31/2014 • pa = a; instead of pa = &a[0] • a[i] can be written as *(pa + i).
  13. 13. 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
  14. 14. Arrays of Pointers • We can have arrays of pointers since pointers are variables. 5/31/2014
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. Questions 5/31/2014
  20. 20. Thank You 5/31/2014

×