You are on page 1of 6
1218123, 12:39 PMA Self Referenial Stucture in C- Sanfoundey Self Referential Structure in C This C Tutorial explains Self Referential Structure in C Programming with examples. A self-referential structure in C is a structure that contains one or more pointers that point to the same type of structure as its member. Consider the structure declaration below, struct NODE { struct NODE new; /* ‘new’ declared variable */ int value; b As we know that structure template tells compiler how to allocate storage to its members and makes computer not to allocate memory to them. When compiler reaches the line struct NODE new; template struct NODE is not fully defined. Further, its member ‘new of type struct NODE contains a member ‘new of type struct NODE which in turn contains a member ‘new’ again of type struct NODE and so on indefinitely. In such an instance, compiler can't evaluate correctly how much storage to allocate to ‘new member of template struct NODE. So we observed here that a member of struct NODE can't be a variable of type struct NODE. Then, how can we make structure struct NODE self- referential? Let's take one more try, this time we declare a ‘pointer-to-struct NODE’ as. a member of template struct NODE, advertisement htipsitwnew.sanfoundry convctutorals-selteterertia-stuctures! 18 1218123, 12:39 PMA Self Referenial Stucture in C- Sanfoundey struct NODE struct NODE “new; /* ‘new’ @ pointer-to-struct NODE */ Ant values b ‘As compiler starts compiling the template struct NODE and reaches line Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now! struct NODE *new; it finds ‘new, a ‘pointer-to-struct NODE’, and also member of struct NODE template, it evaluates correctly how much bytes of storage to be allocated to ‘new’. On linux system, any pointer type takes 8 bytes of storage. There's no problem in using ‘pointer-to-struct NODE’ as a member of struct NODE. Because ‘new is a ‘pointer-to-struct NODE, structure struct NODE is called self- referential structure. typedef struct NODE { struct NODE *news int value; odes Ant main(void) t Node previous, current; /* accessing members of ‘previous’ */ previous.new = Scurrent; /* previous.new is a ‘pointer-to-struct NODE‘ */ previous. value = 100; y In above fragment of code, ‘previous.new is pointing to ‘current’ Node. Self-referential structures have their applications in Advanced Data Structures like, Linked Lists, Binary Trees etc.. advertisement htipsitwnew.sanfoundry convctutorals-selteterertia-stuctures! 216, 1218123, 12:39 PMA nC - Sanfoundey s - 1000 C Tutorials. Sanfoundry Global Education & Learning St Ifyou wish to look at all C Tutorials, go to C Tutorials. advertisement « Prev- How to Access Members ofa Structure —_» Next - Mutually Dependent Structures in C inc? Related Posts: * Practice Computer Science MCQs Apply for C internship Check C Books = Drartica BCA MMe htipsitwnew.sanfoundry convctutorals-selteterertia-stuctures! ais 1218123, 12:39 PMA Self Referenial Stucture in C- Sanfoundey @ C,C- Structure and Union advertisement Recommended Articles: 1. Difference between Structure Pattern, Template and Variable in C 2. Structure in C Programming with Examples 3, Mutually Dependent Structures in C 4. C Programming Questions and Answers - Self-Referential Structures - 2 5. Nested Structure in C with Examples 6. Difference between Structure and Array in C 7. How to Access Members of a Structure in C? 8. How to Pass or Return a Structure from a Function in C 9, Linked List in C with Examples 10. C Programming Questions and Answers - Self-Referential Structures - 1 advertisement htipsitwnew.sanfoundry convctutorals-selteterertia-stuctures! 1218123, 12:39 PMA Sel Referential Stuctur nC - Sanfoundey Additional Resources: «Data Structure MCQ Questions * Java Programs on Classes and Objects * C Tutorial Object Oriented Programming MCQ Questions Linked List Programs in C Popular Pages: * C Programming MCQ Questions + C++ Programming MCQ Questions * C Programming Interview Questions Visual Basic MCQ Questions * C Programs on Arrays Subscribe: C Programming Newsletter Name Email Subscribe Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to. htipsitwnew.sanfoundry convctutorals-selteterertia-stuctures! 56 1218123, 12:39 PMA Self Referenial Stucture in C- Sanfoundey Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn. Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses. About | Certifications | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact © 2011-2023 Sanfoundry. All Rights Reserved. htipsitwnew.sanfoundry convctutorals-selteterertia-stuctures! 86

You might also like