0% found this document useful (0 votes)
2 views

Lec18 Pointers Part2

Chapter 9 discusses pointers in C++, including the importance of initializing pointers to NULL or 0, and how to use pointers as function parameters to modify arguments. It also covers dynamic memory allocation using the 'new' operator and the necessity of releasing memory with 'delete'. Additionally, it explains the concept of constant pointers and pointers to constants.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lec18 Pointers Part2

Chapter 9 discusses pointers in C++, including the importance of initializing pointers to NULL or 0, and how to use pointers as function parameters to modify arguments. It also covers dynamic memory allocation using the 'new' operator and the necessity of releasing memory with 'delete'. Additionally, it explains the concept of constant pointers and pointers to constants.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 9:

Pointers

Copyright © 2012 Pearson Education, Inc.


Null Pointers
– When you begin a program, you may have some
pointers declared, but are not yet set to any value, in
this case you should set them to NULL.
– NULL is a special value that indicates a pointer is
unused.
– For each pointer type, there is a special value -- the
"null pointer" -- which is distinguishable from all other
pointer values and which is not the address of any
object.
– NULL is defined as 0 (zero) in C++.

Copyright © 2012 Pearson Education, Inc.


Null Pointers
• Pointers should be initialized to 0, NULL,
or to an address of some variable (of the
same type).
– int *aPtr = 0;
– int *bPtr = NULL;
• A pointer with a value 0 points to “nothing”.

Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
9.7
Pointers as Function Parameters

Copyright © 2012 Pearson Education, Inc.


Pointers as Function
Parameters
• A pointer can be a parameter
• Works like reference variable to allow change to
argument from within function
• Requires:
1) asterisk * on parameter in prototype and heading
void getNum(int *ptr); // ptr is pointer to an int
2) asterisk * in body to dereference the pointer
*ptr = 100;
3) address as argument to the function
getNum(&num); // pass address of num to getNum

Copyright © 2012 Pearson Education, Inc.


Example
void swap(int *x, int *y)
{ int temp;
temp = *x;
*x = *y;
*y = temp;
}

int num1 = 2, num2 = -3;


swap(&num1, &num2);

• Compare with reference parameters

Copyright © 2012 Pearson Education, Inc.


Passing Arguments to a Function
• In C++, there are three ways to pass
arguments to a function:
– Pass-by-value
– Pass-by-reference with reference arguments
– Pass-by-reference with pointer arguments.

Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
Passing Arguments to a Function
• In C++, there are three ways to pass
arguments to a function:
– Pass-by-value
– Pass-by-reference with reference arguments
– Pass-by-reference with pointer arguments.
• Pass-by-value
– When an argument is passed by value, a copy of
the argument is made, and passed to the function.
– Changes to the copy (inside the function) do NOT
affect the original variable.

Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
Passing Arguments to a Function
• Pass-by-reference with reference arguments
• To pass by reference, you need to use
reference parameters.
• When passing by reference, you give the
function a permission to modify the parameters.
• To indicate that a parameter is passed by
reference, follow the parameter’s type by an
ampersand (&) in the functions’
header/prototype.

Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
• Pass-by-value
int squareByValue(int);
int main()
{ int x = 2;
cout<<" x = "<< x <<" before the
squareByValue\n";
cout<<“squareByValue:
"<<squareByValue(x)<<"\n";
cout<<" x = "<< x <<" after the squareByValue\n";
return 0;
}
int squareByValue(int number)
{
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
• Pass-by-value
int squareByValue(int);
int main()
{ int x = 2;
cout<<" x = "<< x <<" before the
squareByValue\n";
cout<<“squareByValue:
"<<squareByValue(x)<<"\n";
cout<<" x = "<< x <<" after the squareByValue\n";
return 0;
}
x = 2 before the squareByValue
Value returned by squareByValue: 4
int squareByValue(int number)
x = 2 after the squareByValue
{
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
• Pass-by-reference with reference arguments
void squareByReference(int &);
int main()
{
int x = 2;
cout<<" x = "<< x <<" before
squareByReference\n";
squareByReference(x);
cout<<" x = "<< x <<" after squareByReference\n";
return 0;}
void squareByReference(int &numberRef)
{
numberRef *= numberRef;
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
• Pass-by-reference with reference arguments
void squareByReference(int &);
int main()
{
int x = 2;
cout<<" x = "<< x <<" before
squareByReference\n";
squareByReference(x);
cout<<" x = "<< x <<" after squareByReference\n";
return 0;}
x = 2 before the squareByReference
void squareByReference(int &numberRef)
{ x = 4 after the squareByReference
numberRef *= numberRef;
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
• Pass-by-reference with pointer arguments
void squareByReference_withPointer(int *);
int main()
{ int n = 2;
cout<<" n = "<< n <<" before
squareByReference_withPointer\n";
squareByReference_withPointer(&n);
cout<<" n = "<< n <<" after the
squareByReference_withPointer\n";
return 0;}
void squareByReference_withPointer(int *nPtr)
{
*nPtr = *nPtr * *nPtr;
Copyright © 2016
Copyright
Soha © Makady.
2016 Soha
All rights
Makady.
reserved.
All rights reserved.
} Copyright © 2012 Pearson Education, Inc.
• Pass-by-reference with pointer arguments
void squareByReference_withPointer(int *);
int main()
{ int n = 2;
cout<<" n = "<< n <<" before
squareByReference_withPointer\n";
squareByReference_withPointer(&n);
cout<<" n = "<< n <<" after the
squareByReference_withPointer\n";
return 0;}
void
n = 2squareByReference_withPointer(int
before the squareByReference_withPointer *nPtr)
{n = 4 after the squareByReference_withPointer
*nPtr = *nPtr * *nPtr;
Copyright © 2016
Copyright
Soha © Makady.
2016 Soha
All rights
Makady.
reserved.
All rights reserved.
} Copyright © 2012 Pearson Education, Inc.
Pointers to Constants
• If we want to store the address of a
constant in a pointer, then we need to
store it in a pointer-to-const.

Copyright © 2012 Pearson Education, Inc.


Pointers to Constants
• Example: Suppose we have the following
definitions:

const int SIZE = 6;


const double payRates[SIZE] =
{ 18.55, 17.45, 12.85,
14.97, 10.35, 18.89 };
• In this code, payRates is an array of
constant doubles.

9-19
Copyright © 2012 Pearson Education, Inc.
Pointers to Constants
• Suppose we wish to pass the payRates array to
a function? Here's an example of how we can do
it.

void displayPayRates(const double *rates, int size)


{
for (int count = 0; count < size; count++)
{
cout << "Pay rate for employee " << (count + 1)
<< " is $" << *(rates + count) << endl;
}
}

The parameter, rates, is a pointer to const double.

Copyright © 2012 Pearson Education, Inc.


Declaration of a Pointer to
Constant

Copyright © 2012 Pearson Education, Inc.


Constant Pointers
• A constant pointer is a pointer that is
initialized with an address, and cannot
point to anything else.

• Example

int value = 22;


int * const ptr = &value;

Copyright © 2012 Pearson Education, Inc.


Constant Pointers

Copyright © 2012 Pearson Education, Inc.


9.8
Dynamic Memory Allocation

Copyright © 2012 Pearson Education, Inc.


Dynamic Memory Allocation
• Variables & Arrays defined in previous lectures had
static memory allocation; which means that memory
is allocated to them at compile-time.

• But sometimes we need to allocate memory at run-


time.
For example: suppose the user will enter an arrays
of student scores where the number of students is
not known beforehand but rather entered by the
user.
Then you need to allocate your array based on the
value entered by the user at run-time. This is called
Dynamic Memory Allocation.

Copyright © 2012 Pearson Education, Inc.


Dynamic Memory Allocation
• Can allocate storage for a variable while
program is running.
• Computer returns address of newly
allocated variable
• Uses new operator to allocate memory:
double *dptr;
dptr = new double;
• new returns address of memory location

Copyright © 2012 Pearson Education, Inc.


Dynamic Memory Allocation
• Can also use new to allocate array:
double * arrayPtr;
const int SIZE = 25;
arrayPtr = new double[SIZE];
• Can then use [] or pointer arithmetic to access array:
for(i = 0; i < SIZE; i++)
arrayPtr[i] = i * i;
or
for(i = 0; i < SIZE; i++)
*(arrayPtr + i) = i * i;
• Program will terminate if not enough memory available to
allocate

Copyright © 2012 Pearson Education, Inc.


Releasing Dynamic Memory

• Use delete to free dynamic memory:


delete dptr;
• Use [] to free dynamic array:
delete [] arrayPtr;
• Only use delete with dynamic memory!

Copyright © 2012 Pearson Education, Inc.


Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Program 9-14 (Continued)

Copyright © 2012 Pearson Education, Inc.


Notice that in line 49 the value 0 is assigned to the sales pointer. It is a
good practice to store 0 in a pointer variable after using delete on it. First,
it prevents code from inadvertently using the pointer to access the area of
memory that was freed. Second, it prevents errors from occurring if
delete is accidentally called on the pointer again. The delete operator
is designed to have no effect when used on a null pointer.

Copyright © 2012 Pearson Education, Inc.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy