0

I'm passing two c-strings (one containing characters, the other one empty) to a function that is supposed to take the c-string that contains characters and populate the empty c-string with the string read from behind. Simple operation, but it doesn't work. I just can't figure out what I'm doing wrong.

#include <iostream>
#include <cstring>

using namespace std;

void magic(char*, const char*, int);

int main()
{
   char string1[20];
   char string2[] = "This is a test";
   char *str1 = string1; 
   char *str2 = string2;
   int stringsize = strlen(string2);
   magic(str1, str2, stringsize);
   return 0;
}

void magic(char* string1, const char* string2, int stringsize)
{
   cout << string2 << endl; //here it prints as expected
   *(string1 + stringsize) = '\0';
   string1 = string1 + stringsize - 1;
   for(;*string2 != '\0' ;string1--, string2++)
   {
      *string1 = *string2;
   }
   cout << string1 << endl; //blank space is printed - expected "This is a test" reversed
   cout << string2 << endl; //blank space is printed - expected "This is a test" 
}
7
  • Unrelated to your problem, but you do know that arrays decays naturally to pointers? So there's no need for the temporary str1 and str2 variables, just pass the arrays as is to the function. Commented Aug 9, 2015 at 3:39
  • @Joachim. I'm aware, but I wanted to create a function that used pointers instead of simple arrays, so I might have complicated things more than strictly necessary but I still don't understand why it doesn't work? Commented Aug 9, 2015 at 3:50
  • Please decide: Are your strings 0-terminated, or are they counted. Mixing those is generally a bad idea... and your magic-function is very much unsure about it. Commented Aug 9, 2015 at 3:56
  • The function can take pointer still, even if you pass arrays. Even if you declare a function argument as e.g. char string1[20] it will still be a pointer. As for your problem, now might be a good time to learn how to use a debugger, because with a debugger you can step through code, line by line, and watch variables and their values and how they change. That will help you a lot with problems like this. Commented Aug 9, 2015 at 3:56
  • 2
    @WykoW In the magic function, why would you expect string2 to point to the beginning of the string when you are changing where it points to in the loop? That should give you a clue -- print the arrays after magic returns. Commented Aug 9, 2015 at 4:03

1 Answer 1

1

You can see the problem in your logic if you follow the steps in the for loop using a very simple string. Let's say,

string1 = "This";

In the first iteration of the loop,

string1 = "This";
string2 = "T";

In the second iteration of the loop,

string1 = "his";
string2 = "hT";

In the third iteration of the loop,

string1 = "is";
string2 = "ihT";

In the fourth iteration of the loop,

string1 = "s";
string2 = "sihT";

In the fifth iteration of the loop,

string1 = "";
string2 = "?sihT";  // You go past the start of the array.

Hence, after the for loop ends, you need to increment string2 to make sure it points to the start of the array.

void magic(char* string1, const char* string2, int stringsize)
{
   cout << string2 << endl;
   *(string1 + stringsize) = '\0';
   string1 = string1 + stringsize-1;
   for(;*string2 != '\0' ;string1--, string2++)
   {
      *string1 = *string2;
   }

   // Add this.
   string1++;

   cout << string1 << endl; // This is now the reversed string.
   cout << string2 << endl; // This is now the empty string since
                            // you kept incrementing it in the for
                            // loop until you reached the end of the
                            // string.
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great explanation. string1 ended up being out of bounds. I knew it was a a small detail that caused cout to print nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.