1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Programming and Data Structure
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
Pointer definitions, usage, operations, arrays of pointers, function pointers Referencing addresses, Pointers.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Pointers Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Computer Science 210 Computer Organization Pointers.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Computer science C programming language lesson 3.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers What is the data type of pointer variables?
Chapter 8 Arrays, Strings and Pointers
Functions and Pointers
Pointers and Pointer-Based Strings
INC 161 , CPE 100 Computer Programming
Pointers.
Functions and Pointers
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Pointers  Week 10.
Systems Programming Concepts
Pointers Problem Solving & Program Design in C Eighth Edition
Pointers Call-by-Reference CSCI 230
Pointers and Pointer-Based Strings
C Pointers Systems Programming.
Introduction to Pointers
Introduction to Pointers
Presentation transcript:

1 Pointers in C

2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()

3 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

4 Computer Memory Revisited Computers store data in memory slots Each slot has an unique address Variables store their values like this: AddrContentAddrContentAddrContentAddrContent 1000i: j: k: m: a[0]: ‘a’1005a[1]: ‘b’1006a[2]: ‘c’1007a[3]: ‘\0’ 1008ptr: …

5 Computer Memory Revisited Altering the value of a variable is indeed changing the content of the memory e.g. i = 40; a[2] = ‘z’; AddrContentAddrContentAddrContentAddrContent 1000i: j: k: m: a[0]: ‘a’1005a[1]: ‘b’1006a[2]: ‘z’1007a[3]: ‘\0’ 1008ptr: …

6 Addressing Concept Pointer stores the address of another entity It refers to a memory location int i = 5; int *ptr; /* declare a pointer variable */ ptr = &i; /* store address-of i to ptr */ printf(“*ptr = %d\n”, *ptr); /* refer to referee of ptr */

7 Why do we need Pointer? Simply because it’s there! It is used in some circumstances in C Remember this? scanf(“%d”, &i);

8 What actually ptr is? ptr is a variable storing an address ptr is NOT storing the actual value of i int i = 5; int *ptr; ptr = &i; printf(“i = %d\n”, i); printf(“*ptr = %d\n”, *ptr); printf(“ptr = %p\n”, ptr); 5 i address of i ptr Output: i = 5 *ptr = 5 ptr = effff5e0 value of ptr = address of i in memory

9 Twin Operators &: Address-of operator Get the address of an entity e.g. ptr = &j; AddrContentAddrContentAddrContentAddrContent 1000i: j: k: m: ptr:

10 Twin Operators *: De-reference operator Refer to the content of the referee e.g. *ptr = 99; AddrContentAddrContentAddrContentAddrContent 1000i: j: k: m: ptr:

11 Example: Pass by Reference Modify behaviour in argument passing void f(int j) { j = 5; } void g() { int i = 3; f(i); } void f(int *ptr) { *ptr = 5; } void g() { int i = 3; f(&i); } i = ? i = 3i = 5

12 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable5 jintinteger variable10

13 An Illustration int i = 5, j = 10; int *ptr; /* declare a pointer-to-integer variable */ int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable5 jintinteger variable10 ptrint *integer pointer variable

14 An Illustration int i = 5, j = 10; int *ptr; int **pptr; /* declare a pointer-to-pointer-to-integer variable */ ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable5 jintinteger variable10 ptrint *integer pointer variable pptrint **integer pointer pointer variable Double Indirection

15 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; /* store address-of i to ptr */ pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable5 jintinteger variable10 ptrint *integer pointer variableaddress of i pptrint **integer pointer pointer variable *ptrintde-reference of ptr5

16 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; /* store address-of ptr to pptr */ *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable5 jintinteger variable10 ptrint *integer pointer variableaddress of i pptrint **integer pointer pointer variableaddress of ptr *pptrint *de-reference of pptrvalue of ptr (address of i)

17 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable3 jintinteger variable10 ptrint *integer pointer variableaddress of i pptrint **integer pointer pointer variableaddress of ptr *ptrintde-reference of ptr3

18 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable7 jintinteger variable10 ptrint *integer pointer variableaddress of i pptrint **integer pointer pointer variableaddress of ptr **pptrintde-reference of de-reference of pptr 7

19 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable7 jintinteger variable10 ptrint *integer pointer variableaddress of j pptrint **integer pointer pointer variableaddress of ptr *ptrintde-reference of ptr10

20 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable7 jintinteger variable9 ptrint *integer pointer variableaddress of j pptrint **integer pointer pointer variableaddress of ptr **pptrintde-reference of de-reference of pptr 9

21 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable7 jintinteger variable9 ptrint *integer pointer variableaddress of i pptrint **integer pointer pointer variableaddress of ptr *pptrint *de-reference of pptrvalue of ptr (address of i)

22 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table NameTypeDescriptionValue iintinteger variable-2 jintinteger variable9 ptrint *integer pointer variableaddress of i pptrint **integer pointer pointer variableaddress of ptr *ptrintde-reference of ptr-2

23 Pointer Arithmetic What’s ptr + 1 ?  The next memory location! What’s ptr - 1 ?  The previous memory location! What’s ptr * 2 and ptr / 2 ?  Invalid operations!!!

24 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table NameTypeDescriptionValue a[0]floatfloat array element (variable)? a[1]floatfloat array element (variable)? a[2]floatfloat array element (variable)? a[3]floatfloat array element (variable)? ptrfloat *float pointer variable *ptrfloatde-reference of float pointer variable ?

25 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table NameTypeDescriptionValue a[0]floatfloat array element (variable)? a[1]floatfloat array element (variable)? a[2]floatfloat array element (variable)? a[3]floatfloat array element (variable)? ptrfloat *float pointer variableaddress of a[2] *ptrfloatde-reference of float pointer variable ?

26 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table NameTypeDescriptionValue a[0]floatfloat array element (variable)? a[1]floatfloat array element (variable)? a[2]floatfloat array element (variable)3.14 a[3]floatfloat array element (variable)? ptrfloat *float pointer variableaddress of a[2] *ptrfloatde-reference of float pointer variable 3.14

27 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table NameTypeDescriptionValue a[0]floatfloat array element (variable)? a[1]floatfloat array element (variable)? a[2]floatfloat array element (variable)3.14 a[3]floatfloat array element (variable)? ptrfloat *float pointer variableaddress of a[3] *ptrfloatde-reference of float pointer variable ?

28 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table NameTypeDescriptionValue a[0]floatfloat array element (variable)? a[1]floatfloat array element (variable)? a[2]floatfloat array element (variable)3.14 a[3]floatfloat array element (variable)9.0 ptrfloat *float pointer variableaddress of a[3] *ptrfloatde-reference of float pointer variable 9.0

29 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table NameTypeDescriptionValue a[0]floatfloat array element (variable)? a[1]floatfloat array element (variable)? a[2]floatfloat array element (variable)3.14 a[3]floatfloat array element (variable)9.0 ptrfloat *float pointer variableaddress of a[0] *ptrfloatde-reference of float pointer variable ?

30 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table NameTypeDescriptionValue a[0]floatfloat array element (variable)6.0 a[1]floatfloat array element (variable)? a[2]floatfloat array element (variable)3.14 a[3]floatfloat array element (variable)9.0 ptrfloat *float pointer variableaddress of a[0] *ptrfloatde-reference of float pointer variable 6.0

31 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table NameTypeDescriptionValue a[0]floatfloat array element (variable)6.0 a[1]floatfloat array element (variable)? a[2]floatfloat array element (variable)3.14 a[3]floatfloat array element (variable)9.0 ptrfloat *float pointer variableaddress of a[2] *ptrfloatde-reference of float pointer variable 3.14

32 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table NameTypeDescriptionValue a[0]floatfloat array element (variable)6.0 a[1]floatfloat array element (variable)? a[2]floatfloat array element (variable)7.0 a[3]floatfloat array element (variable)9.0 ptrfloat *float pointer variableaddress of a[2] *ptrfloatde-reference of float pointer variable 7.0

33 Pointer Arithmetic and Array Type of a is float * a[2]  *(a + 2) ptr = &(a[2])  ptr = &(*(a + 2))  ptr = a + 2 a is a memory address constant ptr is a pointer variable float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0;

34 More Pointer Arithmetic What if a is a double array? A double may occupy more memory slots! Given double *ptr = a; What’s ptr + 1 then? AddrContentAddrContentAddrContentAddrContent 1000a[0]: …1002…1003… 1004a[1]: …1006…1007… 1008a[2]: …1010…1011…

35 More Pointer Arithmetic Arithmetic operators + and – auto-adjust the address offset According to the type of the pointer: sizeof(double) = = 1004 AddrContentAddrContentAddrContentAddrContent 1000a[0]: …1002…1003… 1004a[1]: …1006…1007… 1008a[2]: …1010…1011…

36 Advice and Precaution Pros Efficiency Convenience Cons Error-prone Difficult to debug

37 Summary A pointer stores the address (memory location) of another entity Address-of operator (&) gets the address of an entity De-reference operator (*) makes a reference to the referee of a pointer Pointer and array Pointer arithmetic