October 2011
Beginner to intermediate
1200 pages
35h 33m
English
Now suppose you want to write a function that returns a string. Well, a function can’t do that. But it can return the address of a string, and that’s more efficient. Listing 7.10, for example, defines a function called buildstr() that returns a pointer. This function takes two arguments: a character and a number. Using new, the function creates a string whose length equals the number, and then it initializes each element to the character. Then it returns a pointer to the new string.
Listing 7.10. strgback.cpp
// strgback.cpp -- a function that returns a pointer to char#include <iostream>char * buildstr(char c, int n); // prototypeint main(){ using namespace std; int times; char ch; cout << ...
Read now
Unlock full access