Problem making board (conected pointers for board spots)

Sooooo i've been trying to make myself a minesweeper in c for school and need to use pointers and connected lists for my project... but not working ;-;
On "mynod" when the printing function is called, it says "header->nextright" is null... it should be connecting to a not null nod, any guesses? I made a page worth of drawings checking connections, so don't think its that, might i be missing something more simple?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  #include<stdio.h>
#include <stdlib.h>
#include <time.h> //will need those for mines later

typedef struct nodmain nodmain;
typedef struct nod nod;

typedef struct nodmain //needed a block with 3 ways out for sides
{
	nod *nextright;
	nodmain *nextdown;
	nodmain *prev;
	int num;
	bool mine;
};

typedef struct nod  //normal in between blocks 2 ways out (one conects to nod other to mainnod since they can have either on the side, counting as 1)
{
	nodmain *prevmain;
	nod *prevnod;
	nod *nextright;
	int num;
	bool mine;
};

nod *Inicializa() //me being lazy
{
	nod init;
	return &init;
}

nodmain *Inicializamain() //me being lazy x2
{
	nodmain init;
	return &init;
}

nodmain *Createboard(int modo, nodmain *header)
{
	int size;
	int mines;
	if (modo == 1)
	{
		size = 10;
		mines = 20;
	}
	else
	{
		size = 10;
		mines = 20;
	}

	nodmain *prevmainnod = Inicializamain();
	nod *prevnod = Inicializa();

	for (int i = 0; i < size; i++)
	{		
		if (header == NULL)
		{
			//first main block
			nodmain *mainnod = Inicializamain();
			mainnod->mine = false;
			mainnod->num = 0;
			mainnod->nextdown = NULL;
			mainnod->nextright = NULL;
			mainnod->prev = NULL;
			prevmainnod = mainnod;
			header = mainnod;
		}
		else
		{
			//other mainblocks
			nodmain *mainnod = Inicializamain();
			mainnod->mine = false;
			mainnod->num = 0;
			mainnod->nextdown = NULL;
			mainnod->nextright = NULL;
			mainnod->prev = prevmainnod;
			prevmainnod->nextdown = mainnod;
			prevmainnod = mainnod;
		}	
		for (int j = 0; j < (size - 1); j++)
		{
			nod *nod = Inicializa();
			if (j == 0)
			{
				//first block
				nod->mine = false;
				nod->num = 0;
				nod->nextright = NULL;
				nod->prevmain = prevmainnod;
				nod->prevnod = NULL;
				prevmainnod->nextright = nod;
				prevnod = nod;
			}
			else
			{
				//other blocks
				nod->mine = false;
				nod->num = 0;
				nod->nextright = NULL;
				nod->prevmain = NULL;
				nod->prevnod = prevnod;
				prevnod->nextright = nod;
				prevnod = nod;
			}
		}
	}
	return header;
}

void PrintBoard(nodmain *header,int modo) //just to check ERROR on mynod=NULL :C
{
	if (modo == 1)
	{
		int size = 10;
	}

	//debug delete after
	int size = 10;

	bool onecycle = false;

	for (int i = 0; i < size; i++)
	{
		nod *mynod;
		onecycle = false;
		printf("%d\t", header->num);
		for (int j = 0; j < size; j++)
		{
			while (onecycle == false)
			{
				mynod = header->nextright;
				onecycle = true;
			}
			printf("%d\t",mynod->num);
		}
		printf("\n");
		header = header->nextdown;
	}
}

int main(void)
{
	nodmain *header = NULL;
	header=Createboard(1,header);
	PrintBoard(header, 1);

	//Ignore down :P

	/*srand(time(NULL));
	int opcao,modo;
	while (opcao != 0)
	{
		printf("/tMinesweeper/t/n");
		printf("Insira a opção:/n");
		printf("1 - Novo Jogo/n");
		printf("0 - Sair/n");
		scanf("%d", &opcao);
		if (opcao == 1)
		{
			printf("Selecione modo: /n");
			printf("1 - Classico (50x50, 20 minas, regras normais/n");
			printf("? - Voltar/n");
			printf("0 - Sair/n");
			scanf("%d", &modo);
			if (modo == 1)
			{

			}
		}
	}*/
}
1
2
3
4
5
6
7
8
9
10
11
nod *Inicializa() //me being lazy
{
	nod init;
	return &init; // Replace with 'new nod'
}

nodmain *Inicializamain() //me being lazy x2
{
	nodmain init;
	return &init; // Replace with 'new nodmain'
}
This is plain wrong. You return a pointer to a local [uninitialized] variable which does not exists any longer after the function ends. Accessing this memory will certainly crash.

Why do you need two types of nodes?

Today the typedef paradigm shouldn't be used anymore...
Thank you, didn't realize the function would return a non existential address ^~^
As to why i'm using 2 different tipedef structs, i might add additional information on nodmain later,
so i'm keeping my possibilities open...
why shouldn't typedef be used anymore? What should i use instead?
why shouldn't typedef be used anymore? What should i use instead?
Nothing, you can use the type directly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
typedef struct nodmain nodmain;
typedef struct nod nod;

typedef struct nodmain //needed a block with 3 ways out for sides
{
	nod *nextright;
	nodmain *nextdown;
	nodmain *prev;
	int num;
	bool mine;
};

typedef struct nod  //normal in between blocks 2 ways out (one conects to nod other to mainnod since they can have either on the side, counting as 1)
{
	nodmain *prevmain;
	nod *prevnod;
	nod *nextright;
	int num;
	bool mine;
};
Last edited on
why shouldn't typedef be used anymore? What should i use instead?

well in this example, as it turned out, you had no need for typedef's at all but even when there's need prefer alias declarations to typedef's: for further details refer 'Effective Modern C++', Herb Sutter; the relevant chapter is also titled 'Prefer alias declarations to typedefs'
Topic archived. No new replies allowed.