1

It works in the Windows Command Prompt like I haven't missed the return new_node;

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

typedef struct Node {
    int value;
    struct Node *next;
 } Node;

Node* create_node(int value) {
    Node *new_node = (Node *) malloc(sizeof(Node));
    if (new_node == NULL) return NULL;
    new_node->value = value;
    new_node->next = NULL;
    // no return statement
}

int main(int argc, char *argv[]) {
    Node *head = NULL;

    // no errors here, head just receives the right pointer
    head = create_node(5);

    return 0;
}

So the function create_node(int) returns pointer anyway. How does it work?

Compiled with gcc (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 7.2.0

sibstudent1
  • 59
  • 1
  • 7

1 Answers1

3

That's undefined behavior and standard mentions it clearly

From §6.9.1¶12 C11 standard

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

Compile your code with all warnings enabled. gcc -Wall -Werror prog.c. In this case you will see compiler mentioning that there is no return statement though it is supposed to return something.

user2736738
  • 30,591
  • 5
  • 42
  • 56