0

I am new to C programming and was wondering if anyone could give me a hint on why my program is acting up on repl.it, but not on CLion. It seems to have something to do with my return statement where I return smallest in my min function. However, I don't understand why it is working correctly on one IDE and not the repl.it IDE.

#include <stdio.h>
#include <string.h>

char *min(char *a, char *b, char *c);

int main(void) {
    char apple[10] = "apple";
    char boy[10] = "boy";
    char zoo[10] = "zoo";
    char smallest[10];

    strcpy(smallest, min(apple, boy, zoo));

    printf("The smallest string is %s", smallest);

    return 0;
}

char *min(char *a, char *b, char *c) {
    char smallest[10];

    int firstResult = strcmp(a, b);

    if (firstResult < 0) {
        strcpy(smallest, a);
    } else {
        strcpy(smallest, b);
    }

    int secondResult = strcmp(smallest, c);

    if (secondResult < 0) {
        strcpy(smallest, smallest);
    } else {
        strcpy(smallest, c);
    }

    return smallest;
}
klutt
  • 30,332
  • 17
  • 55
  • 95
  • "It's not working" is not a proper problem description. How to they differ? – klutt Jun 26 '18 at 20:46
  • The `smallest[]` array is popped off the stack after the return statement. Therefore, you have a dangling pointer after the function call. Your function could just return an int that points to the "smallest" argument passed to the function. For example, return 1 if a is the smallest, 2 if b is smallest, ... – jdc Jun 26 '18 at 20:48
  • 1
    Long story short, the function `min()` is incorrect. It is incorrect in repl.it and it is also incorrect in CLion, it is incorrect everywhere. The part that is incorrect is `return smallest`, because `smallest` only exists for the duration of the function call. – Dietrich Epp Jun 26 '18 at 20:48
  • Im sorry, In Clion I get the output The smallest string is an apple, on repl.it however all I get is an error. Here are screenshots of my different outputs. https://ibb.co/c6yL4o https://ibb.co/eGNPVT – Mystic Developer Jun 26 '18 at 20:49
  • 1
    @MysticDeveloper: The fact that running the program in CLion gave you the right answer is unfortunate, because it makes your job more difficult. If the program crashed, it would be obvious that your program is wrong. However, your program gave the right answer... completely by accident... so even though the program is wrong, it's less obvious that it's wrong. – Dietrich Epp Jun 26 '18 at 20:52
  • Thanks so much Dietrich, I was going crazy thinking it was the repl.it IDE lol – Mystic Developer Jun 26 '18 at 20:52
  • 1
    I recommend enabling warnings, standard compiler warnings will identify this problem (you'll see a message like "warning: function returns address of local variable [-Wreturn-local-addr]"). The Address Sanitizer will also catch this error, I recommend enabling the Address Sanitizer, because it will make programs with errors in them more consistent (they will more consistently crash, which is good, instead of just doing the wrong thing, which is worse). – Dietrich Epp Jun 26 '18 at 20:57

0 Answers0