300 likes | 715 Views
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.
E N D
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 • 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 • 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; • *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?
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 • 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!
prtObject prtObject prtObject ptrNext ptrNext ptrNext Linked Lists • Excellent for handling an unknown number of objects. ptrHead Account Object Account Object 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