C++ Programming Concepts

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

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
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.
Kernighan/Ritchie: Kelley/Pohl:
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Introduction to C Programming CE
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
C++ Programming for Graphics Lecture 5 References, New, Arrays and Destructors.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Fundamental Programming: Fundamental Programming Introduction to C++
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
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.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Lecture 23: Pointers. 2 Lecture Contents: t Pointers and addresses t Pointers and function arguments t Pointers and arrays t Pointer arrays t Demo programs.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
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?
Computer Organization and Design Pointers, Arrays and Strings in C
CSE 220 – C Programming Pointers.
Motivation and Overview
Pointers in C.
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
Student Book An Introduction
Pointers Psst… over there.
Pointers Psst… over there.
Simulating Reference Parameters in C
C++ Pointers and Strings
Pointers and Pointer-Based Strings
C Programming Pointers
Chapter 9: Pointers and String
Arrays and Pointers CSE 2031 Fall May 2019.
C++ Pointers and Strings
Arrays and Pointers CSE 2031 Fall July 2019.
Presentation transcript:

C++ Programming Concepts Lecture 3 Pointers in C/C++

Introduction Basic Concepts Declaring, pointing and dereferencing. “Ordinary” variables Declaring, pointing and dereferencing. Passing by value Classes and Objects Structures Array Linked List

Some Basic Concepts - 1 Should all be familiar with “ordinary” variables declarations. Such as…. int nCount; double dBalance; char chLetter; char arName[50]; and so on…..

Basic Concepts - 2 Should also be familiar with assignments. Such as ….. nCount = 5; dBalance = 12.36; chLetter = ‘Z’; (note single quotes); arName = “David D. Hodgkiss”; ?????? This is wrong – the compiler will complain Discuss this later.

Declare and Assign Should also be familiar with….. int nCount = 4; double dBalance = 12.36; char cLetter = ‘Z’; char arName[] = “David D. Hodgkiss”;

Why pointers? Consider this code (abridged) int main(void) { a = 1; b = 2; swap(a, b); cout << a << b << endl; } void swap(int a, int b) int temp; temp = a; a = b; b = temp; When ‘a’ and ‘b’ printed after swap result will be…. a = 1; b = 2; Why?

Passing values. C does not pass the variable to functions. It passes the value that the variable holds. Within the function new variables are created. This is known as “Pass by Value” In “swap” – it is only the variables within swap() that have their values swapped. Variables lost when function returns.

How can we swap? Using pointers. Pointers Declaration Are themselves variables That hold a memory address instead of a variable value Declaration int *pInt;

Reading the declaration int *pInt; Break the declaration down “pInt” The pointer’s name “*” Indicating it is a pointer “int” Type of pointer So…. “pInt” is a pointer to an integer

Reading the declaration int *pInt; Break the declaration down “pInt” The pointers name “*” Indicating it is a pointer “int” Type of pointer So…. “pInt” is a pointer to an integer

Other declaration examples double *pDouble; char *pChar; int *pMyPointer; long *pYourPointer; and so on

Using pointers The pointer variable holds an address location. From whence do’th this address derive? Consider…. int nCount; // an integer variable int *pInt; // a pointer to an integer Requirement – make pInt “point” at “nCount”

Finding the address We can use the “&” character (address of) So…. pInt = & nCount; // spaces added for clarity pInt equals the address of nCount

Getting at the value How do we get a value via the pointer? Using the ‘*’ character again. To “dereference” the pointer. cout << *pInt << endl; output the value at which “pInt” is pointing.

More accessing myInt = *pInt; arInts[5] = *pInt; *pInt = nCount; *pA = *pB; //is this the same as pA = pB? You should experiment with these concepts myInt = nInt; // What would happen here?

Back to swap How can me make swap work correctly? By using pointers! Instead of swap receiving variable values Pass pointer values to it.

Swapping with pointers swap(&a, &b); // this is in “main” void swap(int *pA, int *pB) { int temp; temp = *pA; *pA = *pB; *pB = temp; }

What about classes Consider that we have a class called “Account” Could instantiate using class Account Fred; Could also use pointers First create a pointer to a class of type Account class Account *pAcc; Now instantiate pAcc = new Account;

pAcc = new Account; “pAcc” is a pointer “new” is a C++ keyword So It allocates a block of memory and passes (returns) the location (address) of that block (containing an Account Object) to pAcc So pAcc is pointing at the Account object. what if “new” fails to allocate?

Interacting with the object When using a pointer we do not use the “dot” notation. We use an arrow “->” That consists of …. a “dash” followed by a “greater than” symbol

Interacting with the object Remember pAcc = new Account; To interact use…… pAcc->SetBalance(12.36); pAcc->SetIntRate(3.4); dBal = pAcc->GetBalance(); dInt = pAcc->GetIntRate();

Big deal – what good is it Let’s have an array of pointers Account *arAccounts[10]; An array called “arAccounts” Has 10 elements Each of which is…… A pointer to…… An object of type Account

Handling objects via an array Some code ….. Account *arAccounts[10]; for(nCount = 0; nCount < 10; nCount++) arAccounts[nCount] = new Account; That will create an array of pointers to ten separate Account objects.

Accessing via an array arAccounts[1]->SetBalance(12.36); arAccounts[1]->SetIntRate(5.36); dBal = arAccounts[1]->GetBalance(): dInt = arAccounts[1]->GetIntRate(); By using an array we need not find names for each instantiation.

What about strings char arName[50]; The actual array name is a pointer. To copy one “string” to another. E.g. contents of arName[] to arCustomer[] ?? arCustomer = arName ?? arCustomer[] = arName[] Neither of these will work

Copying and manipulating strings C provides a number of functions to copy strings Copy strcpy(……..) Concatenate strcat(…….) Compare strcmp(……) Length strlen(……) All of these use pointers.

C++ & Strings C++ can handle strings as objects We will be looking at string handling classes in a later lecture. It hides the implementation!

Linked Lists Excellent for handling an unknown number of objects. ptrHead prtObject ptrNext Account Object prtObject ptrNext Account Object prtObject ptrNext Account Object ???

Further investigation “this” is a pointer To what? Background reading Test by developing some code

Summary Some basic concepts Apply to pointer declaration Finding the address of a variable Dereferencing the pointer Passing by reference – using pointers Handling objects Arrays Linked Lists