c++ programming concepts

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

Upload: enya

Post on 15-Jan-2016

91 views

Category:

Documents


0 download

DESCRIPTION

C++ Programming Concepts. Lecture 3 Pointers in C/C++. Introduction. Basic Concepts “Ordinary” variables Declaring, pointing and dereferencing. Passing by value Classes and Objects Structures Array Linked List. Some Basic Concepts - 1. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: C++ Programming Concepts

C++ Programming Concepts

Lecture 3

Pointers in C/C++

Page 2: C++ Programming Concepts

Introduction

• Basic Concepts– “Ordinary” variables

• Declaring, pointing and dereferencing.

• Passing by value

• Classes and Objects

• Structures– Array– Linked List

Page 3: C++ Programming Concepts

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…..

Page 4: C++ Programming Concepts

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.

Page 5: C++ Programming Concepts

Declare and Assign

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

Page 6: C++ Programming Concepts

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?

Page 7: C++ Programming Concepts

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.

Page 8: C++ Programming Concepts

How can we swap?

• Using pointers.

• Pointers– Are themselves variables– That hold a memory address

• instead of a variable value

• Declaration– int *pInt;

Page 9: C++ Programming Concepts

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

Page 10: C++ Programming Concepts

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

Page 11: C++ Programming Concepts

Other declaration examples

• double *pDouble;

• char *pChar;

• int *pMyPointer;

• long *pYourPointer;– and so on

Page 12: C++ Programming Concepts

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”

Page 13: C++ Programming Concepts

Finding the address

• We can use the “&” character (address of)

• So….– pInt = & nCount; // spaces added for clarity

– pInt equals the address of nCount

Page 14: C++ Programming Concepts

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.

Page 15: C++ Programming Concepts

More accessing

• myInt = *pInt;• arInts[5] = *pInt;• *pInt = nCount;• *pInt = 6;• *pInt = arInts[12];• *pA = *pB; //is this the same as pA = pB?

• You should experiment with these concepts– myInt = nInt; // What would happen here?

Page 16: C++ Programming Concepts

Back to swap

• How can me make swap work correctly?

• By using pointers!

• Instead of swap receiving variable values

• Pass pointer values to it.

Page 17: C++ Programming Concepts

Swapping with pointers

• swap(&a, &b); // this is in “main”

• void swap(int *pA, int *pB){

int temp;temp = *pA;*pA = *pB;*pB = temp;

}

Page 18: C++ Programming Concepts

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;

Page 19: C++ Programming Concepts

pAcc = new Account;

• “pAcc” is a pointer• “new” is a C++ keyword

– 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?

Page 20: C++ Programming Concepts

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

Page 21: C++ Programming Concepts

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

Page 22: C++ Programming Concepts

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

Page 23: C++ Programming Concepts

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.

Page 24: C++ Programming Concepts

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.

Page 25: C++ Programming Concepts

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

Page 26: C++ Programming Concepts

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.

Page 27: C++ Programming Concepts

C++ & Strings

• C++ can handle strings as objects

• We will be looking at string handling classes in a later lecture.– It hides the implementation!

Page 28: C++ Programming Concepts

Linked Lists

• Excellent for handling an unknown number of objects.

ptrHeadprtObject

ptrNextAccount Object

prtObject

ptrNext

prtObject

ptrNext

Account Object

Account Object

???

Page 29: C++ Programming Concepts

Further investigation

• “this” is a pointer

• To what?– Background reading– Test by developing some code

Page 30: C++ Programming Concepts

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