what does the function returns with in C program , in case if there is no return statement in the code

后端 未结 3 1208
情书的邮戳
情书的邮戳 2021-01-24 04:40

I made a function on how to reverse a singly linked list recursively in C.

the function is as below.

struct node * reverseSLL2(struct node *p,struct node         


        
3条回答
  •  悲哀的现实
    2021-01-24 04:49

    This is undefined behavior. Compiling the code should produce a warning about reaching the end of a function through a code path that is missing a return.

    The reason you do not see that the function is broken, and observe it producing the right results, is because the actual value that is returned from the base case branch (i.e. the else) does have a correct return. It is possible that the compiled code reuses that last return value in all invocations of the function up the stack, so the caller ends up getting the intended value.

    However, the code is invalid. You should add return in front of the recursive call to fix the problem:

    return reverseSLL2(temp1,p);
    

提交回复
热议问题