Computer Programming Lecture 14 C/C++ Pointers

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
An introduction to pointers in c
Pointers Pointer is a variable that contains the address of a variable Here P is sahd to point to the variable C C 7 34……
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers in C Rohit Khokher
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
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.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Applications
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
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.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Student Book An Introduction
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
Windows Programming Lecture 02.
Pointers.
Outline Defining and using Pointers Operations on pointers
Pointer Operations.
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Introduction to Pointers
Presentation transcript:

Computer Programming Lecture 14 C/C++ Pointers Greet class

Pointer Variable Declarations and Initialization Why we need Pointers: Pointes enable programs to create dynamic memory, which can grow at runtime using. The main difference between normal variables and pointers is that, variables contain a value, while pointers contain a memory address. Memory address: We know each memory address contain some value. Thus if pointers contain memory addresses, we can get its value (indirectly). C 7 3 4 … 173 172 174 175 176 177 178 179 180 181 P 833 832 834 835 836 837 838 839 840 841 C is a variable Memory P is a Pointer ?

Pointers A pointer variable must be declared before it can be used. Examples of pointer declarations: int *a; float *b; char *c; The asterisk, when used as above in the declaration, tells the compiler that the variable is to be a pointer, and the type of data (the value that we get indirectly) that the pointer points to.

Referencing The unary operator & gives the address of a variable The statement P = &C; assigns the address of C to the variable P, and now P points to C To print a pointer, use %p format.

Referencing int C; int *P; /* Declare P as a pointer to int */ C = 7; P = &C; C … 7 3 4 … 172 173 174 175 176 177 178 179 180 181 P … 174 3 4 … 832 833 834 835 836 837 838 839 840 841

Dereferencing The unary operator * is the dereferencing operator Applied on pointers Access the value of object the pointer points to The statement *P = 5; puts in C (the variable pointed by P) the value 5

Dereferencing printf(“%d”, *P); /* Prints out ‘7’ */ *P = 177; printf(“%d”, C); /* Prints out ‘177’ */ P = 177; /* This is unadvisable!! */ C … 177 7 3 4 … 172 173 174 175 176 177 178 179 180 181 P … 174 177 3 4 … 832 833 834 835 836 837 838 839 840 841

Review All data (software instructions, variables, arrays) is in memory. Each memory location has an address. A pointer is a C version of the address. * denotes we are accessing or storing the value in the address of the pointer. & denotes we are accessing or storing the memory address in the pointer. If any pointer knows the address of any memory address. It can easily change its value (virus concept). Always remember Only O and NULL evaluate to FALSE.

Bits and Bytes Memory is divided into bytes, each of which are further divided into bits Each bit can have a value of 0 or 1 A byte is eight bits Can hold any value from 0 to 255 Memory can be thought of as a sequence of bytes, numbered starting at 0 1 2 3 4 5 6

Storing Variables Each variables is stored in some sequence of bytes Number of bytes depends on what? Number of bytes depends on the data type Can two variables share a byte? Two variables will never share a byte – a variable uses all of a byte, or none of it Example: An int is usually stored as a sequence of two bytes

Using the Address This holds true for every language – each variable has to be stored somewhere In C/C++, you can get and use the address For any variable x, &x returns the address of x

Pointers & Allocation (1/2) After declaring a pointer: int *ptr1; ptr1 doesn’t actually point to anything yet. So its address is NULL. . We can either: make it point to something that already exists, ptr1 = &C or allocate room in memory for something new that it will point to… (dynamic memory will discuss later) ptr = NULL Memory

Pointers & Allocation (2/2) Pointing to something that already exists: int *ptr, var1, var2; var1 = 5; ptr = &var1; var2 = *ptr; var1 and var2 have room implicitly allocated for them.   ptr ? 5 ? ? 5 var1 var2

More C Pointer Dangers void f() { int *ptr; *ptr = 5; } Declaring a pointer just allocates space to hold the pointer – it does not allocate something to be pointed to! Thus the address is NULL. What does the following code do? We can’t store anything in the pointer (ptr) unless ptr contains some address. void f() { int *ptr; *ptr = 5; }

Arithmetic on Pointers A pointer may be incremented or decremented. This means only address in the pointer is incremented or decremented. An integer (can be constant) may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another. Pointer variables can be used in comparisons, but usually only in a comparison to pointer variables or NULL. Instructor: Most logical operations can be used on pointers as with regular variables, but the programmer must be careful of the results since each program doesn’t have free reign of the entire computer’s memory space. Errors which attempt to use memory not assigned to a certain program will cause what will be seen as a segmentation fault or bus error in UNIX, or Windows’ extreme, more deadly blue screen of death. Pointers can be incremented and decremented. Integers can be added or subtracted from a pointer, and pointers variable may be subtracted from one another. Pointers can also be used in comparisons, but usually only in a comparison to NULL as been shown when testing file pointers for a successful file open.

Arithmetic on Pointers When an integer (n) is added to or subtracted from a pointer (ptr) The new pointer value (ptr) is changed by the ptr address plus (+) n multiple (*) the (bytes of ptr data type). ptr + n * (bytes of ptr data type) Example int *ptr; ptr = 1000; ptr = ptr + 2; // ptr address is now changed to 1000 + 2*2 (because integer consumes 2 bytes) New address is now 1004 Instructor: Memory arithmetic is slightly more complicated than standard algebra. Each byte of memory has its own unique address. Remember that different data types use multiple bytes of memory thus span multiple addresses. The UNIX data sizes are reviewed: int: 32 bit – 4 bytes float: 32 bit – 4 bytes double: 64 bit – 8 bytes char: 8 bit – 1 byte So addition of y to a pointer will actually be a multiple of y*x where x is the size of the data type in bytes. In the example shown here, the pointer valptr contains the address of a double precision variable and the address is shown. Each double precision number is 8 bytes. The addition of 2 to the pointer is then 2*8, or 16. Thus we can see the addition of 2 to the pointer adds 16 to the address.

Arithmetic on Pointers Example (2) long *ptr; ptr = 1000; ptr++; 1000 + 1*4 = 1004 Example (3) float *ptr; ptr+=3; 1000 + 3*8 = 1024 Example (4) int *ptr; int num1 = 0; ptr = &num1; ptr++; 1000 + 2 = 1004 Address data 1000 1002 num1 1004 1006 1008 Instructor: Memory arithmetic is slightly more complicated than standard algebra. Each byte of memory has its own unique address. Remember that different data types use multiple bytes of memory thus span multiple addresses. The UNIX data sizes are reviewed: int: 32 bit – 4 bytes float: 32 bit – 4 bytes double: 64 bit – 8 bytes char: 8 bit – 1 byte So addition of y to a pointer will actually be a multiple of y*x where x is the size of the data type in bytes. In the example shown here, the pointer valptr contains the address of a double precision variable and the address is shown. Each double precision number is 8 bytes. The addition of 2 to the pointer is then 2*8, or 16. Thus we can see the addition of 2 to the pointer adds 16 to the address. Memory width is 2 bytes

Arithmetic on Pointers Example (5) void main (void) { int *pointer1, *pointer2; int num1 = 93; pointer1 = &num1; //address of num1 pointer2 = pointer1; // pointer1 address is assign to pointer2 cout << *pointer1 << *pointer2; } Address data 1000 1002 1004 1006 1008 num1 = 93 pointer1 = 1002 pointer2 = 1002 Instructor: Memory arithmetic is slightly more complicated than standard algebra. Each byte of memory has its own unique address. Remember that different data types use multiple bytes of memory thus span multiple addresses. The UNIX data sizes are reviewed: int: 32 bit – 4 bytes float: 32 bit – 4 bytes double: 64 bit – 8 bytes char: 8 bit – 1 byte So addition of y to a pointer will actually be a multiple of y*x where x is the size of the data type in bytes. In the example shown here, the pointer valptr contains the address of a double precision variable and the address is shown. Each double precision number is 8 bytes. The addition of 2 to the pointer is then 2*8, or 16. Thus we can see the addition of 2 to the pointer adds 16 to the address.

Logical operators on Pointers We can apply logical operators (<, >, <=, >=, ==, != ) on pointers. But remember pointers can be compared to pointers or NULL Example (6) int *pointer1, *pointer2; // both pointer2 contains NULL addresses int num1 = 93; If ( pointer1 == NULL ) // pointer compared to NULL pointer1 = &num1; pointer2 = &num1; If ( pointer1 == pointer2 ) // pointer compared to pointer cout << “Both pointers are equal”; Instructor: Memory arithmetic is slightly more complicated than standard algebra. Each byte of memory has its own unique address. Remember that different data types use multiple bytes of memory thus span multiple addresses. The UNIX data sizes are reviewed: int: 32 bit – 4 bytes float: 32 bit – 4 bytes double: 64 bit – 8 bytes char: 8 bit – 1 byte So addition of y to a pointer will actually be a multiple of y*x where x is the size of the data type in bytes. In the example shown here, the pointer valptr contains the address of a double precision variable and the address is shown. Each double precision number is 8 bytes. The addition of 2 to the pointer is then 2*8, or 16. Thus we can see the addition of 2 to the pointer adds 16 to the address.

Passing Pointers to Function(1) We know pointers contains two things (address and value in the address). We can pass the pointers to functions as input parameter or output parameter as By value and By address When we pass the pointers to functions by value. It is called passing by value. When we pass the pointers to functions by address. It is called passing by reference.

Passing Pointers to Function(1) Passing by value (Example) void fun1 (int pass_by_value); void main (void) { int *pointer; int num1 = 10; pointer = &num1; fun1 (*pointer); // Pass by value } void fun1 (int pass_by_value) cout << “Value is “ << pass_by_value;

Passing Pointers to Function(1) Passing by reference (Example) void fun1 (int *pass_by_reference); void main (void) { int *pointer; int num1 = 10; pointer = &num1; fun1 (pointer); // address of num1 is passed fun1 (&num1); // address of num1 is passed cout << num1; } void fun1 (int *pass_by_reference) cout << “Value is “ << *pass_by_value; *pass_by_value = 20; Address data 1000 1002 num1 = 10 1004 1006 pointer = 1002 1008