Pointers an d Arrays - 图1Pointers an d Arrays

Arrays and pointers are closely related in the C++ programming language. You can address arrays as if they were pointers and address pointers as if they were arrays. Being able to store and access pointers and arrays gives you the ability to store strings of data in array elements. Without pointers, you could not store strings of data in arrays because there is no fundamental string data type in C++ (no string variables, only string literals).

This chapter introduces the following concepts:

  • Array names and pointers

  • Character pointers

  • Pointer arithmetic

  • Ragged-edge arrays of string data

This chapter introduces concepts you will use for much of your future programming in C++. Pointer manipulation is important to the C++ programming language.

An array name is a pointer.

Array Names as Pointer s

An array name is just a pointer, nothing more. To prove this, suppose you have the following array declaration:

i nt ar a[ 5] = { 10, 20, 30, 40, 50} ;

If you printed ar a[ 0] , you would see 10. Because you now fully understand arrays, this is the value you would expect.

But what if you were to print * ar a ? Would * ar a print anything? If so, what? If you thought an error message would print because ar a is not a pointer but an array, you would be wrong. An array name is a pointer. If you print * ar a , you also would see 10.

Recall how arrays are stored in memory. Figure 27.1 shows how ar a would be mapped in memory. The array name, ar a , is nothing more than a pointer pointing to the first element of the array. Therefore, if you dereference that pointer, you dereference the value stored in the first element of the array, which is 10. Dereferencing ar a is exactly the same thing as referencing to ar a[ 0] , because they both produce the same value.

Pointers an d Arrays - 图2

Figure 27.1. Storing the array called ar a in memory.

You now see that you can reference an array with subscripts or with pointer dereferencing. Can you use pointer notation to print the third element of ar a ? Yes, and you already have the tools to do so. The following cout prints ar a[ 2] (the third element of ar a ) without using a subscript:

cout << * ( ar a+2) ; / / Pr i nt s ar a[ 2].

The expression * ( ar a+2) is not vague at all, if you remember that an array name is just a pointer that always points to the array’s first element. * ( ar a+2) takes the address stored in ar a , adds two to the address, and dereferences that location. The following holds true:

ar a+0 points to ar a[ 0] ar a+1 points to ar a[ 1] ar a+2 points to ar a[ 2] ar a+3 points to ar a[ 3] ar a+4 points to ar a[ 4]

Pointers an d Arrays - 图3Therefore, to print, store, or calculate with an array element, you can use either the subscript notation or the pointer notation. Because an array name contains the address of the array’s first element, you must dereference the pointer to get the element’s value.

An array name is a pointer constant.

Pointers an d Arrays - 图4

Pointer Advantage s

Although arrays are actually pointers in disguise, they are special types of pointers. An array name is a pointer constant, not a pointer variable. You cannot change the value of an array name, because you cannot change constants. This explains why you cannot assign an array new values during a program’s execution. For instance, even if cname is a character array, the following is not valid in C++:

cname = “ Chr i st i ne Chamber s” ; / / I nvali d arr ay assi gnment.

The array name, cname, cannot be changed because it is a constant. You would not attempt the following

5 = 4 + 8 * 21; / / I nvali d assi gnment

because you cannot change the constant 5 to any other value. C++ knows that you cannot assign anything to 5, and C++ prints an error message if you attempt to change 5. C++ also knows an array name is a constant and you cannot change an array to another value. (You can assign values to an array only at declaration time, one element at a time during execution, or by using functions such as st r cpy() .) This brings you to the most important reason to learn pointers: pointers (except arrays referenced as pointers) are variables. You can change a pointer variable, and being able to do so makes processing virtually any data, including arrays, much more power-

ful and flexible.

Examples

  1. By changing pointers, you make them point to different values in

    memory. The following program demonstrates how to change pointers. The program first defines two floating-point values. A floating-point pointer points to the first variable, v1, and is used in the cout . The pointer is then changed so it points to the second floating-point variable, v2.

/ / Fil ename: C27PTRCH. CPP

/ / Changes t he val ue of a poi nt er var i abl e. #i ncl ude <i ost r eam. h>

#i ncl ude <i omani p. h> voi d mai n()

{

f l oat v1=676. 54; / / Def i nes t wo

f l oat v2=900. 18; / / f l oat i ng- poi nt var i abl es. f l oat * p_v; / Def i nes a f l oat i ng- poi nt poi nt er .

p_v = &v1; / / Makes poi nt er poi nt t o v1. cout << “ The f i r st val ue i s “ << set pr eci si on( 2) <<

* p_v << “ \ n” ; / / Pr i nt s 676. 54.

p_v = &v2; / / Changes t he poi nt er so i t

/ / poi nt s t o v2.

cout << “ The second val ue i s “ << set pr eci si on( 2) <<

Pointers an d Arrays - 图5* p_v << “ \ n” ; / / Pr i nt s 900. 18.

r et ur n;

}

Because they can change pointers, most C++ programmers use pointers rather than arrays. Because arrays are easy to declare, C++ programmers sometimes declare arrays and then use pointers to reference those arrays. If the array data changes, the pointer helps to change it.

  1. Pointers an d Arrays - 图6You

    can use pointer notation and reference pointers as arrays with array notation. The following program declares an integer array and an integer pointer that points to the start of the array. The array and pointer values are printed using subscript notation. Afterwards, the program uses array notation to print the array and pointer values.

Study this program carefully. You see the inner workings of arrays and pointer notation.

/ / Fil ename: C27ARPTR. CPP

/ / Ref er ences arr ays li ke poi nt er s and

/ / poi nt er s li ke arr ays. #i ncl ude <i ost r eam. h> voi d mai n()

{

i nt ct r ;

i nt i ar a[ 5] = { 10, 20, 30, 40, 50} ;

i nt * i pt r ;

i pt r = i ar a; / / Make i pt r poi nt t o arr ay’ s f i r st

/ / el ement . Thi s woul d wor k al so:

/ / i pt r = &i ar a[ 0];

cout << “ Usi ng arr ay subscr i pt s:\ n” ; cout << “ i ar a\t i pt r \ n” ;

f or ( ct r =0; ct r <5; ct r ++)

{ cout << i ar a[ ct r ] << “ \t ” << i pt r [ ct r ] << “ \ n” ; }

cout << “ \ nUsi ng poi nt er not at i on:\ n” ; cout << “ i ar a\t i pt r \ n” ;

f or ( ct r =0; ct r <5; ct r ++)

{ cout << * ( i ar a+ct r) << “ \t ” << * ( i pt r +ct r) << “ \ n” ; }

r et ur n;

}

Here is the program’s output:

Usi ng i ar a

arr ay subscr i pt s: i pt r

10

10

20

20

30

30

40

40

50

50

Usi ng

poi nt er not at i on:

i ar a

i pt r

10

10

20

20

30

30

40

40

50

50

Using Character Pointer s

The ability to change pointers is best seen when working with character strings in memory. You can store strings in character arrays, or point to them with character pointers. Consider the following two string definitions:

char car a[ ] = “ C++ i s f un” ; / / An arr ay hol di ng a st r i ng

Character pointers can point to the first character of a string.

char * cpt r = “ C++ By Exampl e” ; / / A poi nt er t o t he st r i ng

Figure 27.2 shows how C++ stores these two strings in memory. C++ stores both in basically the same way. You are familiar with the array definition. When assigning a string to a character pointer, C++ finds enough free memory to hold the string and assign the address of the first character to the pointer. The previous two string defini- tion statements do almost exactly the same thing; the only difference between them is that the two pointers can easily be exchanged (the array name and the character pointers).

Pointers an d Arrays - 图7Because cout prints strings starting at the array or pointer name until the null zero is reached, you can print each of these strings with the following cout statements:

cout << “ St r i ng 1: “ << car a << “ \ n” ; cout << “ St r i ng 2: “ << cpt r << “ \ n” ;

You print strings in arrays and pointed-to strings the same way. You might wonder what advantage one method of storing strings has over the other. The seemingly minor difference between these stored strings makes a big difference when you change them. Suppose you want to store the string Hell o in the two strings.

You cannot assign the string to the array like this:

car a = “ Hell o” ; / / I nvali d

Because you cannot change the array name, you cannot assign it a new value. The only way to change the contents of the array is by assigning the array characters from the string an element at a time, or by using a built-in function such as st r cpy() . You can, however, make the character array point to the new string like this:

cpt r = “ Hell o” ; / / Change t he poi nt er so

/ / i t poi nt s t o t he new st r i ng.

Pointers an d Arrays - 图8

Figure 27.2. Storing two strings: One in an array and one pointed to by a pointer variable.

Pointers an d Arrays - 图9

Pointers an d Arrays - 图10Examples

  1. Pointers an d Arrays - 图11Suppose

    you want to store your sister’s full name and print it. Rather than using arrays, you can use a character pointer. The following program does just that.

/ / Fil ename: C27CP1. CPP

/ / St or es a name i n a char act er poi nt er . #i ncl ude <i ost r eam. h>

voi d mai n()

{

char * c=” Bett ye Lou Hor n” ;

cout << “ My si st er ’ s name i s “ << c << “ \ n” ; r et ur n;

}

This prints the following:

My si st er ’ s name i s Bett ye Lou Hor n

  1. Pointers an d Arrays - 图12Pointers an d Arrays - 图13Suppose

    you must change a string pointed to by a character pointer. If your sister changed her last name to Henderson, your program can show both strings in the following man- ner:

Identify the program and include the I/O header file. This program uses a character pointer, c , to point to a string literal in memory. Point to the string literal, and print the string. Make the character- pointer point to a new string literal, then print the new string.

/ / Fil ename: C27CP2. CPP

/ / I ll ust r at es changi ng a char act er st r i ng. #i ncl ude <i ost r eam. h>

voi d mai n()

{

char * c=” Bett ye Lou Hor n” ;

cout << “ My si st er ’ s mai den name was “ << c << “ \ n” ;

c = “ Bett ye Lou Hender son” ; / / Assi gns new st r i ng t o c.

cout << “ My si st er ’ s marr i ed name i s “ << c << “ \ n” ; r et ur n;

}

The output is as follows:

My si st er ’ s mai den name was Bett ye Lou Hor n

Pointers an d Arrays - 图14My si st er ’ s marr i ed name i s Bett ye Lou Hender son

  1. Do not use character pointers to change string constants. Doing so

    can confuse the compiler, and you probably will not get the results you expect. The following program is similar to those you just saw. Rather than making the charac- ter pointer point to a new string, this example attempts to change the contents of the original string.

/ / Fil ename: C27CP3. CPP

/ / I ll ust r at es changi ng a char act er st r i ng i mpr oper l y. #i ncl ude <i ost r eam. h>

voi d mai n()

{

char * c=” Bett ye Lou Hor n” ;

cout << “ My si st er ’ s mai den name was “ << c << “ \ n” ;

c += 11; / / Makes c poi nt t o t he l ast name

/ / ( t he t wel ft h char act er) .

c = “ Hender son” ; / / Assi gns a new st r i ng t o c.

cout << “ My si st er ’ s marr i ed name i s “ << c << “ \ n” ; r et ur n;

}

The program seems to change the last name from Horn to Henderson, but it does not. Here is the output of this program:

My si st er ’ s mai den name was Bett ye Lou Hor n My si st er ’ s marr i ed name i s Hender son

Pointers an d Arrays - 图15Why didn’t the full string print? Because the address pointed to by c was incremented by 11, c still points to Hender son , so that was all that printed.

  1. You might guess at a way to fix the previous program. Rather than

    printing the string stored at c after assigning it to Hender son , you might want to decrement it by 11 so it points to its original location, the start of the name. The code to do this follows, but it does not work as expected. Study the program before reading the explanation.

/ / Fil ename: C27CP4. C

/ / I ll ust r at es changi ng a char act er st r i ng i mpr oper l y. #i ncl ude <i ost r eam. h>

voi d mai n()

{

char * c=” Bett ye Lou Hor n” ;

cout << “ My si st er ’ s mai den name was “ << c << “ \ n” ;

c += 11; / / Makes c poi nt t o t he l ast

/ / name ( t he t wel ft h char act er) .

c = “ Hender son” ; / / Assi gns a new st r i ng t o c. c - = 11; / / Makes c poi nt t o i t s

/ / or i gi nal l ocat i on ( ???) .

cout << “ My si st er ’ s marr i ed name i s “ << c << “ \ n” ; r et ur n;

}

This program produces garbage at the second cout . There are actually two string literals in this program. When you first assign c to Bett ye Lou Hor n , C++ reserves space in memory for the constant string and puts the starting address of the string in c.

When the program then assigns c to Hender son , C++ finds room for another character constant, as shown in Figure 27.3. If you subtract 11 from the location of c, after it points to the new string Hender son , c points to an area of memory your program is not using. There is no guarantee that printable data appears before the string constant Hender son . If you want to manipulate parts of the string, you must do so an element at a time, just as you must with arrays.

Pointer Arithmeti c

You saw an example of pointer arithmetic when you accessed array elements with pointer notation. By now you should be com- fortable with the fact that both of these array or pointer references are identical:

ar a[ sub] and * ( ar a + sub)

You can increment or decrement a pointer. If you increment a pointer, the address inside the pointer variable increments. The pointer does not always increment by one, however.

Suppose f _pt r is a floating-point pointer indexing the first element of an array of floating-point numbers. You could initialize f _pt r as follows:

f l oat f ar a[ ] = { 100. 5, 201. 45, 321. 54, 389. 76, 691. 34} ;

f _pt r = f ar a;

Pointers an d Arrays - 图16

Pointers an d Arrays - 图17New string in memory

Figure 27.3. Two string constants appear in memory because two string constants are used in the program.

Figure 27.4 shows what these variables look like in memory. Each floating-point value in this example takes four bytes of memory.

Pointers an d Arrays - 图18

Figure 27.4. A floating-point array and a pointer.

Incrementing a pointer can add more than one byte to the pointer.

If you print the value of * f _pt r , you see 100.5. Suppose you increment f _pt r by one with the following statement:

f _pt r++;

C++ does not add one to the address in f _pt r , even though it seems as though one should be added. In this case, because floating- point values take four bytes each on this machine, C++ adds four to f _pt r . How does C++ know how many bytes to add to f _pt r ? C++ knows from the pointer’s declaration how many bytes of memory pointers take. This is why you have to declare the pointer with the correct data type.

After incrementing f _pt r , if you were to print * f _pt r , you would see 201. 45 , the second element in the array. If C++ added only one to the address in f _pt r , f _pt r would point only to the second byte, 100. 5 . This would output garbage to the screen.

Pointers an d Arrays - 图19

Examples

  1. Pointers an d Arrays - 图20The

    following program defines an array with five values. An integer pointer is then initialized to point to the first element in the array. The rest of the program prints the dereferenced value of the pointer, then increments the pointer so it points to the next integer in the array.

Just to show you what is going on, the size of integer values is printed at the bottom of the program. Because (in this case) integers take two bytes, C++ increments the pointer by two so it points to the next integer. (The integers are two bytes apart from each other.)

/ / Fil ename: C27PTI. CPP

Pointers an d Arrays - 图21/ / I ncr ement s a poi nt er t hr ough an i nt eger arr ay. #i ncl ude <i ost r eam. h>

voi d mai n()

{

i nt i ar a[ ] = { 10, 20, 30, 40, 50} ;

i nt * i p = i ar a; / / The poi nt er poi nt s t o

/ / The st ar t of t he arr ay.

cout << * i p << “ \ n” ;

i p++; / / Two ar e act uall y added.

cout << * i p << “ \ n” ;

i p++; / / Two ar e act uall y added.

cout << * i p << “ \ n” ;

i p++; / / Two ar e act uall y added.

cout << * i p << “ \ n” ;

i p++; / / Two ar e act uall y added.

cout << * i p << “ \ n\ n” ;

cout << “ The i nt eger si ze i s “ << si zeof ( i nt ) ; cout << “ byt es on t hi s machi ne \ n\ n” ;

r et ur n;

}

Here is the output from the program:

10

20

30

40

50

The i nt eger si ze i s t wo byt es on t hi s machi ne

  1. Here is the same program using a character array and a character

    pointer. Because a character takes only one byte of storage, incrementing a character pointer actually adds just one to the pointer; only one is needed because the characters are only one byte apart.

/ / Fil ename: C27PTC. CPP

/ / I ncr ement s a poi nt er t hr ough a char act er arr ay. #i ncl ude <i ost r eam. h>

voi d mai n()

{

char car a[ ] = { ‘ a’ , ‘ b’ , ‘ c’ , ‘ d’ , ‘ e’ } ;

char * cp = car a; / / The poi nt er s poi nt t o

/ / t he st ar t of t he arr ay.

cout << * cp << “ \ n” ;

cp++; / / One i s act uall y added.

cout << * cp << “ \ n” ;

cp++; / / One i s act uall y added.

cout << * cp << “ \ n” ;

cp++; / / One i s act uall y added.

cout << * cp << “ \ n” ;

cp++; / / One i s act uall y added.

cout << * cp << “ \ n\ n” ;

Pointers an d Arrays - 图22cout << “ The char act er si ze i s “ << si zeof ( char) ; cout << “ byt e on t hi s machi ne\ n” ;

r et ur n;

}

  1. The next program shows the many ways you can add to, subtract from,

    and reference arrays and pointers. The pro- gram defines a floating-point array and a floating-point pointer. The body of the program prints the values from the array using array and pointer notation.

/ / Fil ename: C27ARPT2. CPP

/ / Compr ehensi ve r ef er ence of arr ays and poi nt er s. #i ncl ude <i ost r eam. h>

voi d mai n()

{

f l oat ar a[ ] = { 100. 0, 200. 0, 300. 0, 400. 0, 500. 0} ;

f l oat * f pt r ; / / Fl oat i ng- poi nt poi nt er .

/ / Make poi nt er poi nt t o arr ay’ s f i r st val ue.

f pt r = &ar a[ 0] ; / / Al so coul d have been t hi s:

/ / f pt r = ar a;

Pointers an d Arrays - 图23cout << * f pt r << “ \ n” ; / / Pr i nt s 100. 0 f pt r ++; / / Poi nt s t o next f l oat i ng- poi nt val ue. cout << * f pt r << “ \ n” ; / / Pr i nt s 200. 0 f pt r ++; / / Poi nt s t o next f l oat i ng- poi nt val ue. cout << * f pt r << “ \ n” ; / / Pr i nt s 300. 0 f pt r ++; / / Poi nt s t o next f l oat i ng- poi nt val ue. cout << * f pt r << “ \ n” ; / / Pr i nt s 400. 0 f pt r ++; / / Poi nt s t o next f l oat i ng- poi nt val ue. cout << * f pt r << “ \ n” ; / / Pr i nt s 500. 0

f pt r = ar a; / / Poi nt s t o f i r st el ement agai n. cout << * ( f pt r +2) << “ \ n” ; / / Pr i nt s 300. 00 but

/ / does not change f pt r .

/ / Ref er ences bot h arr ay and poi nt er usi ng subscr i pt s. cout << ( f pt r +0) [ 0] << “ “ << ( ar a+0) [ 0] << “ \ n” ;

/ / 100. 0 100. 0

cout << ( f pt r +1) [ 0] << “ “ << ( ar a+1) [ 0] << “ \ n” ;

/ / 200. 0 200. 0

cout << ( f pt r +4) [ 0] << “ “ << ( ar a+4) [ 0] << “ \ n” ;

/ / 500. 0 500. 0

r et ur n;

}

The following is the output from this program:

100. 0

200. 0

300. 0

400. 0

500. 0

300. 0

100. 0

100. 0

200. 0

200. 0

500. 0

500. 0

An array that a character pointer

Arrays of String s

You now are ready for one of the most useful applications of character pointers: storing arrays of strings. Actually, you cannot store an array of strings, but you can store an array of character pointers, and each character pointer can point to a string in memory. By defining an array of character pointers, you define a ragged-

edge array. A ragged-edge array is similar to a two-dimensional

defines is a ragged- edge array.

table, except each row contains a different number of characters (instead of being the same length).

The word ragged-edge derives from the use of word processors. A word processor typically can print text fully justified or with a ragged-right margin. The columns of this paragraph are fully justi- fied, because both the left and the right columns align evenly. Letters you write by hand and type on typewriters (remember what a typewriter is?) generally have ragged-right margins. It is difficult to type so each line ends in exactly the same right column.

All two-dimensional tables you have seen so far have been fully justified. For example, if you declared a character table with five rows and 20 columns, each row would contain the same number of characters. You could define the table with the following statement:

char names[ 5][ 20] ={ {“ Geor ge”} ,

{“ Mi chell e”} ,

{“ Joe”} ,

{“ Mar cus”} ,

{“ St ephani e”} } ;

This table is shown in Figure 27.5. Notice that much of the table is wasted space. Each row takes 20 characters, even though the data in each row takes far fewer characters. The unfilled elements contain null zeros because C++ nullifies all elements you do not initialize in arrays. This type of table uses too much memory.

Pointers an d Arrays - 图24Columns

Rows

Most of the table is wasted

Pointers an d Arrays - 图25Figure 27.5. A fully justified table.

To fix the memory-wasting problem of fully justified tables, you should declare a single-dimensional array of character pointers. Each pointer points to a string in memory, and the strings do not have to be the same length.

Here is the definition for such an array:

char * names[ 5] ={ {“ Geor ge”} ,

{“ Mi chell e”} ,

{“ Joe”} ,

{“ Mar cus”} ,

{“ St ephani e”} } ;

This array is single-dimensional. The definition should not confuse you, although it is something you have not seen. The asterisk before names makes this an array of pointers. The data type of the pointers is character. The strings are not being assigned to the array elements, but they are being pointed to by the array elements. Figure 27.6 shows this array of pointers. The strings are stored elsewhere in memory. Their actual locations are not critical because each pointer points to the starting character. The strings waste no data. Each string takes only as much memory as needed by the string and its terminating zero. This gives the data its ragged-right appearance.

Pointers an d Arrays - 图26

Figure 27.6. The array that points to each of the five strings.

To print the first string, you would use this cout :

cout << * names; / / Pr i nt s Geor ge

To print the second string, you would use this cout :

cout << * ( names+1) ; / / Pr i nt s Mi chell e

Pointers an d Arrays - 图27Whenever you dereference any pointer element with the * dereferencing operator, you access one of the strings in the array. You can use a dereferenced element any place you use a string constant or character array (with st r cpy() , st r cmp() , and so on).

Examples

  1. Pointers an d Arrays - 图28Here

    is a full program that uses the pointer array with five names. The f or loop controls the cout function, printing each name in the string data. Now you can see why learning about pointer notation for arrays pays off!

/ / Fil ename: C27PTST1. CPP

/ / Pr i nt s st r i ngs poi nt ed t o by an arr ay. #i ncl ude <i ost r eam. h>

voi d mai n()

{

char * name[ 5] ={ {“ Geor ge”} , / / Def i nes a r agged- edge

{“ Mi chell e”} , / / arr ay of poi nt er s t o

{“ Joe”} , / / st r i ngs.

{“ Mar cus”} ,

{“ St ephani e”} } ;

i nt ct r ;

f or ( ct r =0; ct r <5; ct r ++)

{ cout << “ St r i ng #” << ( ct r +1) <<

“ i s “ << * ( name+ct r) << “ \ n” ; }

r et ur n;

Pointers an d Arrays - 图29}

The following is the output from this program:

St r i ng #1 i s Geor ge St r i ng #2 i s Mi chell e St r i ng #3 i s Joe

St r i ng #4 i s Mar cus

Pointers an d Arrays - 图30St r i ng #5 i s St ephani e

  1. The following program stores the days of the week in an array. When

    the user types a number from 1 to 7, the day of the week that matches that number (with Sunday being 1) displays by dereferencing the pointer referencing that string.

/ / Fil ename: C27PTST2. CPP

Pointers an d Arrays - 图31/ / Pr i nt s t he day of t he week based on an i nput val ue. #i ncl ude <i ost r eam. h>

voi d mai n()

{

char * days[ ] = {“ Sunday” , / / The seven separ at e set s “ Monday” , / / of br aces ar e opt i onal . “ Tuesday” ,

“ Wednesday” , “ Thur sday” ,

“ Fr i day” ,

“ Sat ur day”} ;

i nt day_num;

do

{ cout << “ What i s a day number ( f r om 1 t o 7) ? “ ; ci n >> day_num;

} whil e (( day_num<1) | | ( day_num>7)) ; / / Ensur es

/ / an accur at e number .

day_num-- ; / / Adj ust s f or subscr i pt. cout << “ The day i s “ << * ( days+day_num) << “ \ n” ;

r et ur n;

}

Review Question s

The answers to the review questions are in Appendix B.

  1. Pointers an d Arrays - 图32What

    is the difference between an array name and a pointer?

  2. If you performed the following statement (assume i poi nt er

points to integers that take four bytes of memory),

i poi nt er += 2;

how many bytes are added to i poi nt er ?

  1. Pointers an d Arrays - 图33Which

    of the following are equivalent, assuming i ar y is an integer array and i pt r is an integer pointer pointing to the start of the array?

    1. i ar y and i pt r

    2. i ar y[ 1] and i pt r +1

    3. i ar y[ 3] and * ( i pt r + 3)

    4. * i ar y and i ar y[ 0]

    5. i ar y[ 4] and * i pt r +4

  2. Why is it more efficient to sort a ragged-edge character array than

    a fully justified string array?

  3. Pointers an d Arrays - 图34Given

    the following array and pointer definition

i nt ar a[ ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ;

i nt * i p1, * i p2;

which of the following is allowed?

  1. i p1 = ar a;

  2. i p2 = i p1 = &ar a[ 3];

  3. ar a = 15;

  4. * ( i p2 + 2) = 15; / / Assumi ng i p2 and ar a ar e equal .

Pointers an d Arrays - 图35Review Exercise s

  1. Pointers an d Arrays - 图36Write a

    program to store your family members’ names in a character array of pointers. Print the names.

  2. Write a program that asks the user for 15 daily stock market

    averages and stores those averages in a floating-point array. Using only pointer notation, print the array forward and backward. Again using only pointer notation, print the highest and lowest stock market quotes in the list.

  3. Modify the bubble sort shown in Chapter 24, “Array Pro- cessing,” so

    that it sorts using pointer notation. Add this bubble sort to the program in Exercise 2 to print the stock market averages in ascending order

  4. Pointers an d Arrays - 图37Write

    a program that requests 10 song titles from the user. Store the titles in an array of character pointers (a ragged- edge array). Print the original titles, print the alphabetized titles, and print the titles in reverse alphabetical order (from Z to A).

Summary

You deserve a break! You now understand the foundation of C++’s pointers and array notation. When you have mastered this section, you are on your way to thinking in C++ as you design your programs. C++ programmers know that C++’s arrays are pointers in disguise, and they program them accordingly.

Being able to use ragged-edge arrays offers two advantages: You can hold arrays of string data without wasting extra space, and you can quickly change the pointers without having to move the string data around in memory.

As you progress into advanced C++ concepts, you will appre- ciate the time you spend mastering pointer notation. The next chapter introduces a new topic called structures. Structures enable you to store data in a more unified manner than simple variables have allowed.