Documente Academic
Documente Profesional
Documente Cultură
Lesson 14
Objectives
Pointers and Functions
Pointers and C-Type Strings
Pointers and Functions
We already have studied that there are three ways to pass arguments to functions: by value, by
reference, and by pointers. If the function is intended to modify variables in the calling program,
their variable can not passed by values, since the function obtain only a copy of the variable.
However, either a reference argument or pointer can be used in this situation.
Passing Simple Variables
Here we will compare the reference argument with pointer.
// passref.cpp
// arguments passed by reference
#include <iostream>
using namespace std;
int main()
{
void centimize(double&); //prototype
{
void centimize(double*); //prototype
return 0;
}
//--------------------------------------------------------------
void centimize(double* ptrd)
{
for(int j=0; j<MAX; j++)
*ptrd++ *= 2.54; //ptrd points to elements of varray
}
The prototype of the function is same as in previous program. Here only fact is utilized that array
name is its address and the meaning of double* is same as double[], but this syntax is not very
common.
Here a question arises: how the compiler will interpret *ptrd++ and *(ptrd++) and how does it
know whether to increment the contents of array or the pointer? This is not due to precedence but
it is because of associativity. Associativity is concerned with whether the compiler performs
operations starting with an operator on the right or an operator on the left. So in expression
*(ptrd++) first compiler will increment the pointer using ++ then the content of incremented
pointer will be displayed. While in this program contents are being altered by multiplication.
See book for sake of demonstrating the further examples of array access.
Pointers and C-Type String
As we have studied earlier, strings are nothing but array of characters. Thus pointer notation can be
applied to the characters in strings, just like arrays.
Pointers to String Constants
Here are some program listings which show how to define the string constant using array and
using pointers.
// twostr.cpp
// strings defined using array and pointer notation
#include <iostream>
using namespace std;
int main()
{
char str1[] = "Defined as an array";
char* str2 = "Defined as a pointer";
cout << str1 << endl; // display both strings
cout << str2 << endl;
// str1++; // can't do this; str1 is a constant
str2++; // this is OK, str2 is a pointer
cout << str2 << endl; // now str2 starts "efined..."
return 0;
}
AJ/Handout 14 -4- Object-Oriented Programming
In many ways these two types are equivalent. Like we can print both by same way, use them as
function arguments, and so on. But there is a subtle difference: str1 is an address—that is a pointer
constant—while str2 is a pointer variable. So str2 can be changed, while str1 cannot. So by
incrementing str2 means that it no longer points to the first character in the string. Also the string
defined using pointer is more flexible as shown in the example.
String as Function Arguments
The following program show a string used as a function argument. The function simply prints the
string, by accessing each character in turn.
// ptrstr.cpp
// displays a string with pointer notation
#include <iostream>
using namespace std;
int main()
{
void dispstr(char*); //prototype
char str[] = "Idle people have the least leisure.";
dispstr(str); //display the string
return 0;
}
//--------------------------------------------------------------
void dispstr(char* ps)
{
while( *ps ) //until null character,
cout << *ps++; //print characters
cout << endl;
}
Here in this example the function dispstr is called by str (a constant value) so a copy of this
constant is created in the function. And this copy is pointer ps, and a pointer can be changed so
function increments it. The expression *ps++ returns the successive characters of the string. The
loop cycles until it found null character. Whose value is 0 which works as false for the loop and it
terminates.
Copying a String Using Pointers
Now in this example we will see how the pointers are used to insert the values in the array.
// copystr.cpp
// copies one string to another with pointers
#include <iostream>
using namespace std;
int main()
{
AJ/Handout 14 -5- Object-Oriented Programming