nullptr

The code bellow seems to enter infinite loop, why is that? Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <stddef.h>

using namespace std;

int main()
{
	struct node{
		int m;
		int l;
		node* p;
	};
	srand(time(0));

	node* n;
	node* t;
	node* h;

	n = new node;
	h = n;
	n->l = rand() % 10;
	n->m = 1;
	t = n;

	for (int i = 2; i < 100; ++i)
	{
		n = new node;
		t->p = n;
		t = n;
		n->l = rand() % 10;
		n->m = i;

	};

	n->p = nullptr;

	t = h;
	while (t!= nullptr)
	{
		cout << t->m << "--" << t->l << '\t';
		t = t->p;
	};
It doesn't enter an infinite loop. It runs just fine.
Last edited on
No, it crushes, every time I go cntl f5.
wait you are right. Everything works fine now, i missed one definition.
Last edited on
Topic archived. No new replies allowed.