So I'm writing a program that asks for a string, then assigns pointers to the head and tail, then reverses them and goes through the line until your string is reversed then outputs the reversed string. I've more or less got it however during the while statement I'm getting error 2440 cannot convert from char * to character. Essentially my head and tail pointer won't store their character in my non pointer "temp" character variable.
Help!
Thanks.
#1 is that all of your assignments are backwards. Remember that the var on the left side of the = is what gets changed.
#2 is that temp is a char while tail is a char*. These are two different types (one is a character and one is a pointer) and therefore you cannot directly assign them like this.
You need to "dereference" the pointer which gives you the value that the pointer points to. You can do this with the * operator:
temp = *tail;
This tells the compiler "I want to assign whatever tail points to into temp". Vs. what you were doing, which was trying to assign tail into temp.
#3 is this line:
head = tail;
While this is syntactically legal, it is not what you want to do, for the same reason as #2. Here you are just changing the pointer, not the data that the pointer points to.
head = &myStr[0];
tail = &myStr[strlen(myStr) - 1];
is already wrong. myStr can be an empty string. So its length will be equal to 0. In this case expression strlen(myStr) - 1 will give you an incorrect result.
It is much better when the initial value of tail points out after the last element of a sequence that is in your case it will point out the terminating zero. So it would be much better to write
1 2
head = myStr;
tail = myStr + strlen( myStr );
This loop
while ( tail >= head, ++head, --tail)
will be executed until --tail will be equal to 0.
Also the body of the loop is incorrect. You are trying to assign an object of type char to a pointer.
So this actually works and is an amalgamation of everyone's input. I've tried altering the for statement to make it a while and mostly goofed it. Can you guys think of a way to make this while statement function?
Thanks again for all the helpful advice!
while ( *head <= *tail)
{
temp = *tail;
*tail = *head;
*head = temp;
++*head;
--*tail;
}
This is the best functioning one that i have. It doesn't function.
it outputs the string but doesn't reverse it and then outputs the unreversed string.