Declaring a struct inside the same struct definition | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Declaring a struct inside the same struct definition

How can I declare a struct inside the same struct definition: typedef struct{ int x; int y; point* nearest; } point; In this snippet I would like to have a pointer to the nearest point structure for each point structure, but it doesn't run. How can I do this?

1st Sep 2020, 12:47 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
1 Antwort
+ 3
Look carefully You are not declaring the same structure inside the structure, instead you are declaring a pointer variable pointing to same structure. Such thing is known as self referencing structure. It is quite usefull while implementing more complicated data structures like linked lists Learn more about it here👇 https://www.geeksforgeeks.org/self-referential-structures/
1st Sep 2020, 12:58 PM
Arsenic
Arsenic - avatar