When I use the size of function in the main function it works correctly. However when i use it in the reverse function it only returns 8. Any ideas why or did i incorrectly use it.(This is the beginning of a assignment where I must reverse a string passed to the reverse function. I may only pass the string and nothing else and cannot use the string.h library. If there is another way to accomplish this please let me know. I was planning on using the sizeof function to get the size then looping through the array and use a temp variable to reverse it as I loop.)
#include <stdio.h>
char reverse(char *x);
int main(int argc, char* argv[])
{
char word[] = "Apple pies";
printf("%s\n", word);
reverse(word);
printf("%s\n", word);
printf("%s\n", reverse(word));
printf("%s\n", word);
return 0;
}
char reverse(char *x)
{
char crud;
int lc =0;
size_t length = 0;
char *tmp = x;
while (*tmp++)
++length;
while (length > lc)
{
crud = x[length];
x[length] = x[lc];
x[lc] =crud;
length--;
lc++;
}
}