0

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++;

    }
}
John
  • 49
  • 7
  • 2
    In `reverse`, `x` is just a pointer (i.e. `char*`). In `main`, `x` is an array (`char[11]`). That `sizeof` trick only works on arrays, not pointers. You'll have to pass the length to `reverse` as a parameter (or use `strlen`). – Cornstalks Jan 20 '17 at 03:49
  • I assume strlen is in the library? i need to reverse a string passed to reverse but need to only pass the parameters of (char * x) and cannot use the string.h library – John Jan 20 '17 at 03:51
  • 1
    In that case you have to write your own version of `strlen`. It's easy to do. e.g. `size_t length = 0; while (*x++) ++length;` – Cornstalks Jan 20 '17 at 03:53
  • @Cornstalks could you post that as an answer so i can say you solved it? – John Jan 20 '17 at 03:56

2 Answers2

1

In the main method when you call the sizeof function, you are passing an array. But in the 'reverse' function, the argument is a character pointer. When you call sizeof on a pointer, there is no guaranteed result. Often you get 4 when you call sizeof on a char pointer. But again, there is no guarantee that sizeof will always return that value. You should not use sizeof for pointers.

VHS
  • 9,534
  • 3
  • 19
  • 43
  • When arrays are passed as pointers into functions, `sizeof` only computes the size of the pointer. In general, many functions that manipulate arrays containing binary data also need an explicit length to be passed in. – rlee827 Jan 20 '17 at 04:00
1

In reverse, x is just a pointer (that is, a char*). But in main, x is an array (that is, a char[11]). That sizeof trick only works on arrays, not pointers. You'll have to pass the length to reverse as a parameter (or use strlen). This is covered in more detail in another question and its answers.

Since you say strlen is forbidden in your situation (as is passing in the length via a parameter), you'll have to implement strlen manually. It's trivial:

size_t length_of_str(const char *x) {
    size_t length = 0;
    while (*x++) ++length;
    return length;
}
Community
  • 1
  • 1
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • I updated my code. Do you know whats wrong with the reverse function? – John Jan 20 '17 at 04:03
  • 1
    @christian: You should ask a new question rather than changing your current one. Edits are okay to clarify details, but this is kinda fundamentally changing the original question. I'll give you a hint: `x[length]` is not what you think it is (on the very first iteration). If you need further help, I encourage you to ask a new question. – Cornstalks Jan 20 '17 at 04:07