























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























































Pointer Variable Declarations andInitialization Can declare pointers to any data type Pointer initialization^ ◦ Initialized to 0 , NULL , or address 0 or NULL points to nothing
yPtr y 5 yptr 500000 600000 y 600000 5 address ofy is valueof yptr
code
Using the
and
operators. 3 #include
int a;
a is an integer 11 int *aPtr;
aPtr is a pointer to an integer 1213 a
aPtr
&a;
aPtr assigned address of a 1516 cout
"The address of a is
&a 17
"\nThe value of aPtr is
aPtr; 1819 cout
"\n\nThe value of a is
a 20
"\nThe value of *aPtr is
*aPtr; 2122 cout
"\n\nShowing that
and
are inverses of
"each other.\n&*aPtr
&*aPtr 24
"\n*&aPtr
*&aPtr
endl; 25
and & are inverses of eachother
return
indicates successful termination 2728
end main
and & are inverses; same result when both appliedto aPtr
code
Cube a variable using pass-by-value. 3 #include
prototype 9 10 int main() 11
int number
cout
"The original value of number is
number; 1516
pass number by value to cubeByValue 17 number
cubeByValue( number
cout
"\nThe new value of number is
number
endl; 2021 return
indicates successful termination 2223
end main 24 Pass number by value;result return ed by cubeByValue
code
Cube a variable using pass-by-reference 3
with a pointer argument. 4 #include
prototype 1011 int main() 12
int number
cout
"The original value of number is
number; 1617
pass address of number to cubeByReference 18 cubeByReference( &number
cout
"\nThe new value of number is
number
endl; 2122 return
indicates successful termination 2324
end main 25 Apply address operator & to pass address of numberto cubeByReference cubeByReference modified variable number Prototype indicatesparameter is pointer to int
calculate cube of *nPtr; modifies variable number in main 27 void cubeByReference( int *nPtr
*nPtr
*nPtr
*nPtr
*nPtr;
cube *nPtr 3031
end function cubeByReference
cubeByReference receives address of int variable,i.e., pointer to an int Modify and access int variable usingindirection operator
code
Converting lowercase letters to uppercase letters 3
using a non-constant pointer to non-constant data. 4 #include
prototypes for islower and toupper 1011 void convertToUppercase( char
int main() 14
char phrase[]
"characters and
cout
"The phrase before conversion is:
phrase; 18 convertToUppercase( phrase
cout
"\nThe phrase after conversion is:
phrase
endl; 2122 return
indicates successful termination 2324
end main 25 Parameter is nonconstantpointer to nonconstant data convertToUppercase modifies variable phrase
convert string to uppercase letters 27 void convertToUppercase( char *sPtr
while
*sPtr
current character is not
if
islower( *sPtr
if character is lowercase, 32 *sPtr
toupper( *sPtr
convert to uppercase 3334 ++sPtr;
move sPtr to next character in string 3536
end while 3738
end function convertToUppercase
Parameter sPtr nonconstant pointer tononconstant data Function islower returns true if character is lowercaseFunction toupper returns correspondinguppercase character iforiginal characterlowercase; otherwise toupper returns original When operator(uppercase) character ++ applied to pointer that points toarray, memory addressstored in pointer modifiedto point to next element ofarray.
sPtr cannot modify the character to which it points, 24
i.e., sPtr is a "read-only" pointer 25 void printCharacters( const char *sPtr
for
*sPtr
sPtr++
no initialization 28 cout
*sPtr; 2930
end function printCharacters
sPtr is nonconstant pointer to constant data;cannot modify character towhich sPtr points. Increment sPtr to point to next character.
code
Attempting to modify data through a 3
non-constant pointer to constant data. 4 5 void f( const int
prototype 6 7 int main() 8
int y; 1011 f( &y
f attempts illegal modification 1213 return
indicates successful termination 1415
end main 1617
xPtr cannot modify the value of the variable 18
to which it points 19 void f( const int *xPtr
*xPtr
error: cannot modify a const object 2223
end function f
Parameter is nonconstantpointer to constant data.Pass address of int variable y to attempt illegal modification.Attempt to modify const object pointed to by xPtr . Error produced whenattempting to compile.