Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String reversal in C: What is it I am doing wrong?

Tags:

c

string

I am amidst reading K&R C, mainly to brush my C skills, and while attempting to code a program to reverse a given string, and I have a bug that plagues, which, worst of all, I am unable to debug - nor have a clue what might be the cause of it.

My code is the following:

#include <stdio.h>
#include <string.h>

char * reverse(char *string);

int main(int argc, char *argv[])
{
    printf("Please input a string: \t");

    char string[256];

    scanf("%s", string);

    char *reversed = reverse(string);

    printf("The reversed string is %s\n", reversed);

    return 0;
}

char * reverse(char string[])
{
    int size = strlen(string);
    printf("DEBUG: The size of the string that we got as input was: %d\n", size);
    int counter;
    char reversed[size + 1];

    for(counter = size - 1; counter >= 0; counter--) {
        reversed[size - counter] = string[counter];
        printf("DEBUG: The character copied now was %c and was at index %d\n", string[counter], counter);
    }

    reversed[size + 1] = '\0';

    printf("DEBUG: The reversed string is %s\n", reversed);

    return reversed;
}

(Please forgive the debug statements that litter the code logic. Apart from that, feel free to correct any mistake you might see, and also feel free to make suggestions to improve it)

Now, my code is working (for the most part) but the bug is that it copies characters that I have not input. Below are the (funny) results of two test runs:

The first one:

nlightnfotis@crunchbang:~/SoftwareExperiments$ ./reverse
Please input a string:  fotis
DEBUG: The size of the string that we got as input was: 5
DEBUG: The character copied now was s and was at index 4
DEBUG: The character copied now was i and was at index 3
DEBUG: The character copied now was t and was at index 2
DEBUG: The character copied now was o and was at index 1
DEBUG: The character copied now was f and was at index 0
DEBUG: The reversed string is $sitof
The reversed string is $sitof

(Notice the $)

and the second one:

nlightnfotis@crunchbang:~/SoftwareExperiments$ ./reverse
Please input a string:  lol
DEBUG: The size of the string that we got as input was: 3
DEBUG: The character copied now was l and was at index 2
DEBUG: The character copied now was o and was at index 1
DEBUG: The character copied now was l and was at index 0
DEBUG: The reversed string is lol
The reversed string is lol

More accurately depicted here:

The bug

Could someone more knowledgeable and experienced than me explain to me what's wrong with my code, or maybe give me a hint as to why I am facing this frustrating bug?

like image 678
NlightNFotis Avatar asked Nov 28 '22 16:11

NlightNFotis


1 Answers

You are returning a local variable:

char * reverse(char string[]) {    
  char reversed[size + 1];
  ....
  return reversed;
}

The local variable reversed which was allocated on the stack, ceases to exist once the function reverse returns. So any reference to it from main leads to undefined behavior.

To fix this you can do either of the following:

  1. Make the function void and modify the input array.

  2. Declare the array reversed as static so that its lifetime changes to the lifetime of the program.

  3. Dynamically allocate (and later de-allocate) reversed

like image 59
codaddict Avatar answered Dec 16 '22 09:12

codaddict