2

I need to find a way to reverse a string using pointers in C. I'm not supposed to use array notation or string library functions either.

Here is my current code:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    //Reverse2
    char *forward = "123";
    char *back = malloc(sizeof(char) * 4);
    reverse2(forward, back);

    return 0;
}

void reverse2(char *string, char *string2) {
    int counter = 0;
    char *string1 = string;
    while (*string1) {
        counter += 1;
        string1 += 1;
    }
    string1--;
    int i;
    for (i = 0; i <= counter; i++) {
        *string2 = *string1;
        string2 += 1;
        string1--;
    }
    printf("%s", string2);
}

This is the error it gives when I run it in ideone:

Compilation error   #stdin compilation error #stdout 0s 0KB

prog.c:8:2: warning: implicit declaration of function 'reverse2' is invalid in C99 [-Wimplicit-function-declaration]
        reverse2(forward, back);
        ^
prog.c:41:6: error: conflicting types for 'reverse2'
void reverse2(char* string, char* string2){
     ^
prog.c:8:2: note: previous implicit declaration is here
        reverse2(forward, back);
        ^
1 warning and 1 error generated.
7
  • You are not setting the '\0'-terminating byte for string2. Commented Apr 25, 2018 at 21:10
  • 2
    Add void reverse2(char* string, char* string2); before main to get rid of the warning. Commented Apr 25, 2018 at 21:12
  • One option would be to write the whole reverse2 function before main. But some times it is not as easy as that, for example with cycling dependencies or when you are using a function from another compile unit. The line I suggested is called a function prototype, it's basically telling the compiler "hey, there is a function called reverse2 that takes to pointers to char". So even if the compiler hasn't seen the code of the function, by the time it reaches it's first call, it already knows if the arguments match the specification. Commented Apr 25, 2018 at 21:19
  • 1
    For the record, you don't need malloc at all to solve this problem. I'm pretty sure your professor will grade the answer higher if you do the reverse in place. Commented Apr 25, 2018 at 21:21
  • 1
    Aside: please don't name variables as string string1 string2 and so on. They are no better than i j k. Make the code readable with something like void reverse2(char* source, char* dest) and get rid of the unnecessary local string1. Commented Apr 25, 2018 at 22:37

3 Answers 3

4

Your compilation error is due to the fact that you call reverse2 in main, but the C compiler doesn't know inside of main what reverse2 is yet because reverse2 is defined after main. (The C compiler must be able to compile a .c file in a streaming fashion, so this means that everything it needs to compile something - like a function - must have been defined beforehand)

If you invert the order of the two functions (i.e. you put reverse2 before main), or add this declaration:

void reverse2(char*, char*);

before main() then that error won't happen.

(I haven't checked your code, so I don't know if it does what you want it to do, but your question is about the compilation error: not whether the code actually inverts the string)

Sign up to request clarification or add additional context in comments.

Comments

3

1 - prog.c:8:2: warning: implicit declaration of function 'reverse2' is invalid in C99 [-Wimplicit-function-declaration] reverse2(forward, back);

This error is because you need to define reverse2() before main tries to call it, simply move your code for reverse2() prior to the code for main().

2 - you need to null terminate your backwards string in reverse2()

for (i = 0; i <= counter; i++) {
    *string2 = *string1;
    string2 += 1;
    string1--;
}
*string2 = '\0';

3 - it does no good trying to print out string2 in reverse2() because it points to the end of the string. Print back in main() and you will see the expected results.

reverse2(forward, back);
printf("%s", back);

Comments

1

To prevent these useful warnings, you should either declare or define the reverse2 function before you call it from main().

Furthermore reverse2 does not set the null terminator in the destination string and does not print the reversed string at all, since the pointer string2 was advanced to the end of the reversed string.

Unless the prototype is specified, you should consider allocating space for the reversed string in reverse2 and returning the pointer:

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>

char *reverse2(const char *src) {
    const char *p = src;
    char *dest, *q;

    while (*p) {
        p++;
    }
    dest = malloc(p - src + 1);
    if (dest != NULL) {
        q = dest;
        while (p > src) {
            *q++ = *--p;
        }
        *q = '\0';
    }
    return dest;
}

int main(void) {
    const char *forward = "123";
    char *back = reverse2(forward);
    printf("%s\n", back);
    free(back);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.