0

This question might be stupid, but I've been wondering what happens if you have:

struct List {
    int x;
    List *next;
};
struct List *start = nullptr;
struct List *tmp = start->next; 

What happens to tmp?

I've tried compiling it and I get no errors. If I output address of start I get 0, but if I output tmp I get nothing.

Tomaž
  • 59
  • 1
  • 2
  • 5

2 Answers2

0

On the line struct List *tmp = start->next; since we know that start == nullptr you are dereferencing a nullptr value. This is UB (undefined behavior) and as such "anything" can happen.

Bo R
  • 2,334
  • 1
  • 9
  • 17
-1

It is an undefined behavior. After multiple execusion it may output a different result or maybe a segmentation fault.

Ebrahim
  • 109
  • 1
  • 3