11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

134Part II: Becoming a Functional C++ Programmerint* pInts[10];Given the preceding declaration, pnInt[0] is a pointer to an int value. Thus,the following is true:void fn(){int n1;int* pInts[3];pInts[0] = &n1;*pInts[0] = 1;}orvoid fn(){int n1, n2, n3;int* pInts[3] = {&n1,&n2,&n3};for (int i = 0; i < 3; i++){*pInts[i] = 0;}}or evenvoid fn(){int* pInts[3] = {(new int),(new int),(new int)};for (int i = 0; i < 3; i++){*pInts[i] = 0;}}The latter declares three int objects off the heap.This type of declaration isn’t used very often except in the case of an array ofpointers to character strings. The following two examples show why arraysof character strings are useful.Utilizing arrays of character stringsSuppose I need a function that returns the name of the month corresponding toan integer argument passed to it. For example, if the program is passed a 1, itreturns a pointer to the string “January”; if 2, it reports “February”, and soon. The month 0 is assumed to be invalid as are any numbers greater than 12.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!