I'm teaching myself C, and wrote this string reverse function. I cannot figure out why it is crashing...

#include <stdio.h>


void ReverseString(char *inStr, int length);

void ReverseString(char *inStr, int length)
{
char temp;
int j=length;
int i=0;
	while( i != j)
	{
		temp = *(inStr+i);
		*(inStr+i) = *(inStr+j);
		*(inStr+j) = temp;
		i++;
		j--;
	
}
}



int main()
{

	char mike[] = "Hello";
	ReverseString(mike, 5);
	printf("%s", mike);


}
Aia commented: Good first post with proper c code tags. Proper code format as well. Stick around! +8

At some point it has to stop, and it does when it reach memory that doesn't like been overwritten.

while( i != j)

That never stops. i and j never are the same.

EDIT: Oops... Aia beat me while I was typing. Here's my post anyhow.

Strings in C are arrays of characters, but the thing that makes it a 'string' is the fact that it ends in a null byte.

"Hello" = ['H','e','l','l','o','\0']

Your reversal method starts by swapping mike[0] and mike[5]. This brings the null byte to the beginning of the string. This will result in the string being considered 'empty'. You need to set j to length-1 in your method to avoid this.

But that's not why it's crashing. The real culprit is your while loop. If you fix the error above and run your code, you note that it works on "Hello"... but not on a string with an even number of printable characters, like "Hello!". Trace the flow of your program...

mike = ['H','e','l','l','o','!','\0']
swap mike[0] and mike[5]
swap mike[1] and mike[4]
swap mike[2] and mike[3]
swap mike[3] and mike[2]
/* ... continue for a little while... */
swap mike[9] and mike[-4]

See the issue? Your loop should be while ( i < j ) . As you can see, this will prevent the two ends from passing each other by.

commented: For a well explained post +8

i++;
j--;

If j==5 and i==0
and we increment i at the same time as we decrement j...

after the 1st iteration:
i = 1
j = 4

2nd iteration:
i = 2
j = 3

3rd iteration:
i = 3
j = 2

And your loop doesn't stop until i and j are equal...do you see a problem here? I did not compile it, so you may have other issues as well, but that's the main one I see.

edit: Aia and Ishara both beat me while I was typing. //hard to type when someone's beating you, that's why I'm so slow...lol

Thanks! I never thoughts the j and i would just pass each other like that...

Take care.

int j=length;

should be:

int j = length - 1;

and

while(i !-= j)

should be:
while(i < j)

#include <stdio.h>

void ReverseString(char *inStr, int length);

void ReverseString(char *inStr, int length)
{
char temp;
int j=length-1;
int i=0;
    while( i < j)
    {
        temp = *(inStr+i);
        *(inStr+i) = *(inStr+j);
        *(inStr+j) = temp;
        i++;
        j--;

}
}



int main()
{

    char mike[] = "Hello";
    ReverseString(mike, 5);
    printf("%s", mike);


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.