0
void expand_combinations(const char *remaining_string, string const & s, int rema
in_depth)
{
    if(remain_depth==0)
    {
        std::cout << s << std::endl;
        return;
    }

    for(int k=0; k < strlen(remaining_string); ++k)
    {
        string str(s);
        str.append(1, remaining_string[k]);
        expand_combinations(remaining_string+k+1, str, remain_depth - 1); // what?
    }
    return;
}

On the call to the function, it's passing a string + an integer. What does that become?

node ninja
  • 31,796
  • 59
  • 166
  • 254
  • 4
    It's called pointer arithmetic. Perhaps you might benefit from [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list). – James McNellis Sep 26 '10 at 01:37
  • I know about that stuff I just forgot since I've been using Objective-C lately. – node ninja Sep 26 '10 at 01:42
  • Objective-C has pointer arithmetic, too, and it works exactly how you showed in both languages. – Peter Hosey Sep 27 '10 at 08:03

2 Answers2

5

remaining_string is not a string; it's a pointer to a character. Therefore adding an integer to it simply moves the pointer.

For example, if char *blah = "hello", then blah+1 would point to "ello".

houbysoft
  • 32,532
  • 24
  • 103
  • 156
  • remaining_string is not a pointer to a string; it's a pointer to a character. – R. Martinho Fernandes Oct 14 '11 at 01:33
  • @R.MartinhoFernandes: technically, yes, edited. Kind of nit-picking here, though, imho :) It is common to think of `char *` in C as pointers to "strings", even though obviously they technically point only to the first character. – houbysoft Oct 14 '11 at 02:26
3

It's passing a pointer to the k+1th character. As it descends into the recursion, each call starts farther and farther into the string.

bmargulies
  • 97,814
  • 39
  • 186
  • 310