Documente Academic
Documente Profesional
Documente Cultură
8
Pointers and
Pointer-Based
Strings
8.1 Introduction
Pointers
Powerful, but difficult to master
Can be used to pass arguments to a function
Can be used to create and manipulate dynamic data
structures
Have close relationship with arrays and strings
Pointer declarations
* indicates variable is a pointer
Example
int *myPtr;
Declares pointer to int, of type int *
Multiple pointers require multiple asterisks
int *myPtr1, *myPtr2;
Pointer initialization
Initialized to 0, NULL, or an address
0 or NULL points to nothing (null pointer)
Outline
10
fig08_04.cpp
(1 of 2)
Variable aPtr is
a point to an int
11
14
void f ( int * );
void f ( const int * );
int main ( )
{
int x = 10;
int main ( )
{
int x = 10;
f ( &x) ;
return 0;
f ( &x) ;
return 0;
}
void f ( int *xPtr)
{
int y = 20;
*xPtr = 100 ; // Permitted
xPtr = &y; // Permitted
}
}
void f ( const int *xPtr )
{
int y = 20;
*xPtr = 100 ; // Not permitted
xPtr = &y; // Permitted
}
#include <iostream.h>
#include <iostream.h>
void f ( int * const);
int main ( )
{ int x = 10;
f ( &x) ;
return 0;
f ( &x) ;
return 0;
}
}
void f ( int * const xPtr)
{ int y = 20;
*xPtr = 100 ; // Permitted
xPtr = &y; // Not Permitted
}
17
18
19
20
21
22
23
24
25
Pointer assignment
Pointer can be assigned to another pointer if both are of
same type
If not same type, cast operator must be used
Exception
Pointer to void (type void *)
Generic pointer, represents any type
No casting needed to convert pointer to void *
(void *ptr; int *y, x=5; y = &x;
ptr=y;)
Casting is needed to convert void * to any other
type
void pointers cannot be dereferenced
26
Pointer comparison
Use equality and relational operators
Compare addresses stored in pointers
Comparisons are meaningless unless pointers point to
members of the same array
27
f ( v) ;
28
29
String assignment
Character array
char color[] = "blue";
Creates 5 element char array color
Last element is '\0'
Outline
30
4 using std::cout;
5 using std::endl;
fig08_21.cpp
6
7 void copy1( char *, const char * ); // prototype
8 void copy2( char *, const char * ); // prototype
(1 of 2)
9
10 int main()
11 {
12
char string1[ 10 ];
13
14
char string3[ 10 ];
15
16
17
18
19
20
21
22
23 } // end main
31
fig08_21.cpp
(2 of 2)
Use pointer notation to copy string
in s2 to character array in s1
Observations:
Function calls in the case of pass-by-value and pass-by-reference using
reference parameter are identical. Thus from the function calls it is not
possible to determine which type of these two calls are being used. In the
above function calls in both cases are: modify ( a ). Further inside the
function parameter is being used identically, as b in the above function
modify. We can determine type of parameter passing from the function
prototypes or function header.
Function parameter passing using reference parameter and pointers may
also be distinguished from the function prototype or function header. Thus
& in the prototype indicates reference parameter and * in the prototype
indicates parameter passing by pointers.
2. Returning a reference
Do not return a reference to a local automatic variable, since such a variable will
be destroyed as soon as the execution of the function is completed.
Example:
In the following example the & sign in the function prototype and the
header indicates that a reference is being returned.
int &modify ( int )
int main ( )
{
int a = 4;
// In the following call c
// becomes a reference to x.
//Then c outputs the value of x.
int &c = modify ( a );
cout << c << endl;
return 0;
}
int &modify (int b)
{ static int x = 3;
b += 2;
x += b ;
return x ;
}
Comment: In the above function returning a reference to variable x works because
static variables exist for the entire duration of the program. On the other hand,
returning a reference to the variable b would not work since it is an automatic local
variable.
3.Returning a pointer
Do not return a pointer to a local automatic variable, since such a variable will be destroyed as soon
as the execution of the function is completed.
Example:
In the following example the * sign in the function prototype and header indicates that a
pointer is being returned.
int *modify ( int )
int main ( )
{ int *c = 0;
int a = 4;
// In the following c is assigned
// the address of x.
//The referencing c outputs
// the value of x.
c = modify ( a );
cout << *c << endl;
return 0;
}
int *modify (int b)
{ static int x = 3;
b += 2;
x += b ;
return &x ;
}
Comment: In the above function returning a pointer to variable x works because static variables exist for
the entire duration of the program. On the other hand, returning a reference to the variable b would not
work since it is an automatic local variable.