-1

according to what i learned about function variables they should not 'live' outside their function scope,thus returning a local function variable should cause an error, i do get compiling warnings that i do expect, but it makes no sense to me because the program do work and i am little confused

i wrote the following program:

    int * test(int x) {
    int f=x+1;
    return f; //- int to pointer conversion,idk why this works 
}

int main () {
    int x = 10;
    printf("%d\n",test(x));
}

I first expected the program to crash because we make conversion from int type to pointer at function return, but apparently the program prints 11 which i did not expect, i know this is really bad to write like this anyways it was just a coincidence i wrote something like this and it worked and I had alot of questions after that

the warnings i expected to get :

Solution.c: In function ‘test’:
Solution.c:10:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
     return f;
            ^
Solution.c: In function ‘main’:
Solution.c:15:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("%d\n",test(x));
             ~^    ~~~~~~~
             %ls

can anyone explain how this program works ?

Yarin
  • 13
  • 3
  • You're not returning the variable itself, you're returning its (converted) value. The illegal conversion from `int` (the value of `f`) to `int*` (the return type of the function) causes the compiler to complain. – pmg Aug 24 '20 at 09:28
  • "returning a local function variable should cause an error" No, what happens when you do is simply not defined. Anything can happen. – Lundin Aug 24 '20 at 09:28
  • Your example is not valid C and will not compile on a conforming compiler though... did you mean `return &f;`? – Lundin Aug 24 '20 at 09:29
  • Duplicate: [“Pointer from integer/integer from pointer without a cast” issues](https://stackoverflow.com/questions/52186834/pointer-from-integer-integer-from-pointer-without-a-cast-issues) – Lundin Aug 24 '20 at 09:30
  • again this program compiles and working, and it is printing 11 as i DID NOT expect it to work exactly because the reasons you said thats why i have got really confused about this and asked how this program works when it shouldn't ? , i know that in this function case i need to return the address of the variable and even then that wouldn't work because the variable is local to that function scope but the weird thing is that actually returning the variable as is works and i know it shouldn't :( – Yarin Aug 24 '20 at 09:30
  • Enable all your compiler warnings, turn them into errors even (eg: `gcc ... -Wall -Wextra -Werror ...`) – pmg Aug 24 '20 at 09:33
  • Why do you think, it shouldn't work? The most important part of "undefined behaviour" is that you mustn't expect any defined behaviour. Also "should not work" is an expectation that is not covered. Adding multiple wrong instructions might by accident cancel each out. Or not. It can also work 100 times and bite you in the back when you run the 101st time. – Gerhardh Aug 24 '20 at 09:45
  • @Yarin No it doesn't _compile cleanly_ nor is it working. Please study this: https://software.codidact.com/questions/277340 – Lundin Aug 24 '20 at 09:46
  • Please note that an *error" does not mean you program crashes; it means that your program *may* not give the results it is expected to give all the time. Or (AFAIR): "If you made an error *and* your are lucky, your program crashes every time; if you are unlucky it only crashes very seldom" – U. Windl Aug 24 '20 at 10:05

1 Answers1

0

You tell the compiler that function test() should return return a pointer to an integer. You don't. You return a value of an integer.

The compiler says, "You're probably doing unintentional stuff here, just sayin'. I'll cast this integer to a pointer for you."

Then the compiler says, "You want to print the value of an integer here but what you're giving is a pointer. Just sayin'. I'll cast this pointer to an integer for you."

stderr
  • 143
  • 5
  • Nah the compiler is saying: "This isn't valid C, here is some non-C binary with undefined results that's anyone's guess. Feel free to look at non-standard extension documentation for gcc, it is lacking information about what such a binary will do." After which it might format your hard drive for all we know. – Lundin Aug 24 '20 at 09:49
  • The compiler doesn't cast to integer. It will pass a pointer to the function and then `printf` will only take the number of bytes used for an `int` whatever number of bytes were passed by the caller. Depending on endianess and pointer size that might be very different than what a cast would do. What the compiler says is "You are cheating on printf function by putting garbage in. Don't complain if it fires back at you." – Gerhardh Aug 24 '20 at 09:51
  • @Gerhardh true. In fact, compilers doesn't cast anything really. I just wanted to explain this as pedagogic as possible. – stderr Aug 24 '20 at 09:55
  • so basically the compiler saw my mistakes and did hes own conversions to back me up, but actually that really had me confused because it worked and i expected it NOT to work :( – Yarin Aug 24 '20 at 10:08
  • @Yarin Then don't ignore warnings. If you're compiling with gcc, use the -Werror which will lead gcc to treat warnings as errors. – stderr Aug 24 '20 at 10:17
  • yep so i was using linux and GCC, and when i tested it in a Visual studio compiler it complained and DID not compile this as expected, thank you this made it clear :) – Yarin Aug 24 '20 at 10:21