C Tutorial
One structure can be nested within another structure.
In programming, we usally deal with collection of ints, chars and floats rather than isolated entities.
Example an entity book is a collection of things like a title, an author, total pages, date of publication and price etc..
Structure is user defined data type which combines dissimilar data types into an entity.
Nested Strucure syntax,
struct <struct-name1> { //data-type member1; //data-type member2; ... //data-type membern; }; struct <struct-name2> { //data-type member1; //data-type member2; ... //data-type membern; // <struct-name1> inner; };
Sample nested structure c program,
#include<stdio.h> void main() { struct person { char *firstname, *lastname; }; struct book { int id; char *title; struct person author; float price; }b2, b1 = {1, "book1", {"author1 fn", "lastname1"}, 20.5}; b2.id = 2; b2.title = "book2"; b2.author.firstname = "author2 fn"; b2.author.lastname = "lastname2"; b2.price = 35.0; printf("\nbook id\ttitle \t auther \t price \n"); printf("%d\t%s\t%s %s\t%f\n", b1.id, b1.title, b1.author.firstname, b1.author.lastname, b1.price); printf("%d\t%s\t%s %s\t%f\n", b2.id, b2.title, b2.author.firstname, b2.author.lastname, b2.price); }
Output:
$ cc structure.c $ ./a.out book id title auther price 1 book1 author1 fn lastname1 20.500000 2 book2 author2 fn lastname2 35.000000
Sample c program to copy the nested structure variable to another same structure variable.
here nested structure with string variable using pointers (firstname and last name),
#include<stdio.h> void main() { struct person { char *firstname, *lastname; }; struct book { int id; char *title; struct person author; float price; }b2, b1 = {1, "book1", {"author1 fn", "lastname1"}, 20.5}; b2 = b1; b2.id = 2; b2.title = "book2"; b2.price = 35.0; printf("\nbook id\ttitle \t auther \t price \n"); printf("%d\t%s\t%s %s\t%f\n", b1.id, b1.title, b1.author.firstname, b1.author.lastname, b1.price); printf("%d\t%s\t%s %s\t%f\n", b2.id, b2.title, b2.author.firstname, b2.author.lastname, b2.price); }
Output:
$ cc structure.c $ ./a.out book id title auther price 1 book1 author1 fn lastname1 20.500000 2 book2 author1 fn lastname1 35.000000
here nested structure with string variable using array,
#include<stdio.h> void main() { struct person { char firstname[20], lastname[20]; }; struct book { int id; char *title; struct person author; float price; }b2, b1 = {1, "book1", {"author1 fn", "lastname1"}, 20.5}; b2 = b1; b2.id = 2; b2.title = "book2"; b2.price = 35.0; printf("\nbook id\ttitle \t auther \t price \n"); printf("%d\t%s\t%s %s\t%f\n", b1.id, b1.title, b1.author.firstname, b1.author.lastname, b1.price); printf("%d\t%s\t%s %s\t%f\n", b2.id, b2.title, b2.author.firstname, b2.author.lastname, b2.price); }
Output:
$ cc structure.c $ ./a.out book id title auther price 1 book1 author1 fn lastname1 20.500000 2 book2 author1 fn lastname1 35.000000
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page