Click here to load reader

C++ Programming Concepts

  • View

  • Download

Embed Size (px)


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

Text of C++ Programming Concepts

  • C++ Programming ConceptsLecture 3Pointers in C/C++

  • IntroductionBasic ConceptsOrdinary variablesDeclaring, pointing and dereferencing.Passing by valueClasses and ObjectsStructuresArrayLinked List

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

  • Basic Concepts - 2Should 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 complainDiscuss this later.

  • Declare and AssignShould also be familiar nCount = 4;double dBalance = 12.36;charcLetter = Z;chararName[] = David D. Hodgkiss;

  • Why pointers?Consider this code (abridged)

    int main(void){a = 1; b = 2;swap(a, b);cout

  • 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 ValueIn swap it is only the variables within swap() that have their values swapped.Variables lost when function returns.

  • How can we swap?Using pointers.PointersAre themselves variablesThat hold a memory addressinstead of a variable valueDeclarationint *pInt;

  • Reading the declarationint *pInt;Break the declaration downpInt The pointers name*Indicating it is a pointerintType of pointerSo.pInt is a pointer to an integer

  • Reading the declarationint *pInt;Break the declaration downpInt The pointers name*Indicating it is a pointerintType of pointerSo.pInt is a pointer to an integer

  • Other declaration examplesdouble*pDouble;char*pChar;int*pMyPointer;long*pYourPointer;and so on

  • Using pointersThe pointer variable holds an address location.From whence doth this address derive? nCount;// an integer variableint *pInt;// a pointer to an integer Requirement make pInt point at nCount

  • Finding the addressWe can use the & character (address of)So.pInt = & nCount; // spaces added for clarity

    pInt equals the address of nCount

  • Getting at the valueHow do we get a value via the pointer?Using the * character again.To dereference the pointer.


  • More accessingmyInt = *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 conceptsmyInt = nInt;// What would happen here?

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

  • Swapping with pointersswap(&a, &b);// this is in main

    void swap(int *pA, int *pB) { int temp; temp = *pA; *pA = *pB; *pB = temp; }

  • What about classesConsider that we have a class called AccountCould instantiate usingclass Account Fred;Could also use pointersFirst create a pointer to a class of type Accountclass Account *pAcc;Now instantiatepAcc = new Account;

  • pAcc = new Account;pAcc is a pointernew is a C++ keywordIt allocates a block of memoryandpasses (returns) the location (address) of that block (containing an Account Object) to pAccSopAcc is pointing at the Account object.what if new fails to allocate?

  • Interacting with the objectWhen using a pointer we do not use the dot notation.We use an arrow ->That consists of .a dash followed by agreater than symbol

  • Interacting with the objectRememberpAcc = new Account;To interact usepAcc->SetBalance(12.36);pAcc->SetIntRate(3.4);dBal = pAcc->GetBalance();dInt = pAcc->GetIntRate();

  • Big deal what good is itLets have an arrayof pointersAccount *arAccounts[10];An array called arAccountsHas 10 elementsEach of which isA pointer toAn object of type Account

  • Handling objects via an arraySome 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 arrayarAccounts[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 stringschar 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 stringsC provides a number of functions to copy stringsCopystrcpy(..)Concatenatestrcat(.)Comparestrcmp()Lengthstrlen() All of these use pointers.

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

  • Linked ListsExcellent for handling an unknown number of objects.ptrHeadAccount ObjectAccount ObjectAccount Object???

  • Further investigationthis is a pointerTo what?Background readingTest by developing some code

  • SummarySome basic conceptsApply to pointer declarationFinding the address of a variableDereferencing the pointerPassing by reference using pointersHandling objectsArraysLinked Lists


Search related