Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

CSCI 171 Presentation 11 Pointers. Pointer Basics.
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Pointers Ethan Cerami Fundamentals of Computer New York University.
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. 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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
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()
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.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Computer science C programming language lesson 3.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CS1010 Programming Methodology
CS1010 Programming Methodology
User-Written Functions
Chapter 8 Arrays, Strings and Pointers
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSE 220 – C Programming Pointers.
UNIT 5 C Pointers.
CSCI206 - Computer Organization & Programming
Functions and Pointers
Introduction to the C Language
Hassan Khosravi / Geoffrey Tien
Pointers in C.
CS1010 Programming Methodology
Pointers and Pointer-Based Strings
Pointers.
Student Book An Introduction
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
Lecture 6 C++ Programming
void Pointers Lesson xx
Pointers.
Functions and Pointers
Introduction to the C Language
Introduction to the C Language
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Object Oriented Programming COP3330 / CGS5409
Instructor: Ioannis A. Vetsikas
Pointers  Week 10.
Systems Programming Concepts
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Pointers Problem Solving & Program Design in C Eighth Edition
Programming with Pointers
Pointers Call-by-Reference CSCI 230
Pointers.
Objectives You should be able to describe: Addresses and Pointers
Overloading functions
Pointers Pointers point to memory locations
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
Chapter 9: Pointers and String
Pointers and dynamic objects
C Pointers Systems Programming.
Introduction to Pointers
Presentation transcript:

Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”

Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf() Due to the nature of this event, I am expecting some prior knowledge in the C programming language such as...

Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary Here is the outline of this talk.

Computer Memory Revisited Computers store data in memory slots Each slot has an unique address Variables store their values like this: Addr Content 1000 i: 37 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘c’ 1007 a[3]: ‘\0’ 1008 ptr: 1001 1009 … 1010 1011

Computer Memory Revisited Altering the value of a variable is indeed changing the content of the memory e.g. i = 40; a[2] = ‘z’; Addr Content 1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘\0’ 1008 ptr: 1001 1009 … 1010 1011 Is there any questions so far?

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 */ As its name suggests, … Syntactically, a pointer variable is declared as a type identifier followed by an asterisk “*”.

Why do we need Pointer? Simply because it’s there! It is used in some circumstances in C Remember this? scanf(“%d”, &i); In fact, pointers are commonly encountered elements in C. Do you remember the simple but mysterious good old friend scanf() statement when you learnt the standard I/O facilities in C?

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

Twin Operators &: Address-of operator Get the address of an entity e.g. ptr = &j; Addr Content 1000 i: 40 1001 j: 33 1002 k: 58 1003 m: 74 1004 ptr: 1001 1005 1006 1007

Twin Operators *: De-reference operator Refer to the content of the referee e.g. *ptr = 99; Addr Content 1000 i: 40 1001 j: 99 1002 k: 58 1003 m: 74 1004 ptr: 1001 1005 1006 1007

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); The program segment on the left is demonstrating “pass by value”, i.e. the value, actually a copy, of the variable i is passed as argument to function f(). The other program shows you “pass by reference”. The address of the variable i is passed to f() instead, thus allowing us to modify the content stored in the variable. i = 3 i = ? i = ? i = 5

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 5 j 10

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 5 j 10 ptr int * integer pointer variable

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 5 j 10 ptr int * integer pointer variable pptr int ** integer pointer pointer variable Double Indirection

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 5 j 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable *ptr de-reference of ptr

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 5 j 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *pptr de-reference of pptr value of ptr (address of i)

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 3 j 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr de-reference of ptr

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 7 j 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr **pptr de-reference of de-reference of pptr

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 7 j 10 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr *ptr de-reference of ptr

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 7 j 9 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr **pptr de-reference of de-reference of pptr

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable 7 j 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *pptr de-reference of pptr value of ptr (address of i)

An Illustration Data Table Name Type Description Value i int 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 Name Type Description Value i int integer variable -2 j 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr de-reference of ptr

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

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 Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] a[3] ptr float * float pointer variable *ptr de-reference of float pointer variable

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 Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] a[3] ptr float * float pointer variable address of a[2] *ptr de-reference of float pointer variable

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 Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] 3.14 a[3] ptr float * float pointer variable address of a[2] *ptr de-reference of float pointer variable

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 Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] 3.14 a[3] ptr float * float pointer variable address of a[3] *ptr de-reference of float pointer variable

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 Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] 3.14 a[3] 9.0 ptr float * float pointer variable address of a[3] *ptr de-reference of float pointer variable

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 Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] 3.14 a[3] 9.0 ptr float * float pointer variable address of a[0] *ptr de-reference of float pointer variable

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 Name Type Description Value a[0] float float array element (variable) 6.0 a[1] ? a[2] 3.14 a[3] 9.0 ptr float * float pointer variable address of a[0] *ptr de-reference of float pointer variable

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 Name Type Description Value a[0] float float array element (variable) 6.0 a[1] ? a[2] 3.14 a[3] 9.0 ptr float * float pointer variable address of a[2] *ptr de-reference of float pointer variable

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 Name Type Description Value a[0] float float array element (variable) 6.0 a[1] ? a[2] 7.0 a[3] 9.0 ptr float * float pointer variable address of a[2] *ptr de-reference of float pointer variable

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; 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

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? Addr Content 1000 a[0]: 37.9 1001 … 1002 1003 1004 a[1]: 1.23 1005 1006 1007 1008 a[2]: 3.14 1009 1010 1011

More Pointer Arithmetic Arithmetic operators + and – auto-adjust the address offset According to the type of the pointer: 1000 + sizeof(double) = 1000 + 4 = 1004 Addr Content 1000 a[0]: 37.9 1001 … 1002 1003 1004 a[1]: 1.23 1005 1006 1007 1008 a[2]: 3.14 1009 1010 1011

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

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