-2

Sorry for the probably ridiculous and basic question (I haven't programmed in C since I was a teenager, and I'm 39 now...), but with the code below I get the output from the tcc compiler:

test.c:15: warning: assignment makes pointer from integer without a cast
test.c:26: error: lvalue expected

Why is this happening on line 26?

Thanks for your patience, I mainly do web front and back end stuff these days...

----- CODE -----

#include <stdio.h>
// needed for gets() and puts()

char* input() {

    char inp[256];

    gets(inp);

    return inp;
}

void output(outp) {

    puts(outp);
}

int main() {

    int exe = 1;
    char inp[256];
    char exit_msg[] = "END OF PROGRAM. THANK YOU!\0";

    while(exe) {

        inp = input(); // line 26
        output(inp);

        if (inp == "END"){

            exe = 0;
        }
    }
    puts(exit_msg);

    return 0;
}
sepp2k
  • 363,768
  • 54
  • 674
  • 675

1 Answers1

3

There's so much wrong ...

  1. Don't use gets, ever. It's been removed from C because it's impossible to use safely. (If the input is too long, you have a buffer overflow on your hands.) You could use e.g. fgets instead.

  2. input() returns the address of a local variable. Local variables are destroyed when they go out of scope, i.e. when input returns. The return value is always garbage.

  3. outp is missing a type. Looks like you're getting implicit int from your compiler (implicit int has been removed from C in 1999).

  4. String literals are implicitly NUL-terminated. \0 doesn't do much in "END OF PROGRAM. THANK YOU!\0" (except making sure there are two NULs at the end).

  5. You cannot assign to arrays in C. inp = ... is invalid. Check out strcpy and memcpy. (Even if you could assign to arrays, inp = input() would be a type error because input returns a pointer, not an array (but you can't return arrays).)

  6. inp == "END" compares pointers. inp will never have the same address as a string literal. Check out strcmp.

  7. Why does exe exist? Instead of setting exe = 0, you could just break out of the loop.

(#5 is the one your question is about.)

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Thanks. I will investigate forthwith. I appreciate that someone took the time to answer... – Lee David O'Dea May 12 '18 at 16:53
  • I tried while(1) and using both "break" and "continue" to leave the loop, they both compiled under tcc, but both crashed after "input()", seemingly on execution of "output()".... I'm aware continue is wrong, but I tried it anyway when break; failed.... – Lee David O'Dea May 12 '18 at 17:18