0
typedef struct node {
   int data;
   struct node *next;
} Node;
    
void checkEmpty(Node *list) {
  printf(list == NULL ? "true" : "false");
}

The first time, I created the main() function, made a list directly in it, and called checkEmpty(). It printed true.

int main() {
    Node *list;
    checkEmpty(list); // it return true
    return 0;
}

Then, I created a new function menu(), created a list inside it, and called checkEmpty(). It printed false.

void menu() {
    Node *list;
    checkEmpty(list);
}

int main() {
    menu(); // it return false
    return 0;
}  

Why does this happen?

Mr.Young
  • 324
  • 5
  • 14
Tamil
  • 311
  • 4
  • 9
  • 4
    You are not initialising `list`, all bets are off. – Paul Sanders Aug 24 '21 at 22:33
  • 4
    `list` is uninitialised and thus contains an indeterminate value. The resulting behaviour is thus not predictable - in both cases. Change to `Node *list = NULL;`. – kaylum Aug 24 '21 at 22:33
  • 1
    Does this answer your question? [Uninitialized variable behaviour in C++](https://stackoverflow.com/questions/30172416/uninitialized-variable-behaviour-in-c) – kaylum Aug 24 '21 at 22:37
  • Unrelated: There's no need for the `typdef` trick in C++. C++ learned a lot from C and this is one of the things it learned and baked into the language. `struct Node { int data; Node *next; };` is all you need in order to be able to use `Node` throughout the program. – user4581301 Aug 24 '21 at 22:37
  • 1
    Do not tag both C and C++ except when asking about differences or interactions between the two languages. – Eric Postpischil Aug 24 '21 at 22:39
  • BTW, you misspelled "empty" – Barmar Aug 24 '21 at 22:43

2 Answers2

2

The declaration Node *list inside a function does not initialize list. Its value is indeterminate, meaning it does not have to behave as if it has any fixed value at all. The compiler may generate code that uses a different value for it each time it is used, such as reading from memory on one occasion or using a value from a processor register on another.

To fix your program, give list an initial value with Node *list = 0; or Node *list = NULL.

Also turn on compiler warnings and elevate warnings to errors. Check your compiler documentation for the switches to do that. Also pay attention to compiler warning and error messages.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

You have a pointer of type Node that is currently not pointing to any memory specifically. However, there can already be junk data stored into the memory block of the pointer so that technically it's not pointing to NULL and it is pointing to something. The first time you created a pointer, could've been by chance that it's already NULL in the memory block.

So,if you do declare the pointer, it is always good practice to have the pointer point to NULL first.

Node *list = NULL;
Kraego
  • 2,978
  • 2
  • 22
  • 34
  • Initializing the pointer with NULL also gives you the change to check if the pointer is initialized, remember when the memory is freed you can/should also set the pointer to NULL – Kraego Aug 25 '21 at 05:28