Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » C Language » Structure in C program with example | Nested structure in C

  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Chhath Puja Story
    Chhath Puja History : क्यों मनाते हैं छठ महापर्व Festival
  • Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी
    Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी Biography
  • Why is Makar Sankranti celebrated
    Why is Makar Sankranti celebrated? Festival
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
    Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी Biography
  • State the Universal law of gravitation | Law of Gravity, Formula
    State the Universal law of gravitation | Law of Gravity, Formula Science
  • जन्माष्टमी व्रत पूजा विस्तार से | दही हांडी | Krishna Janmashtami Puja
    जन्माष्टमी व्रत पूजा विस्तार से, दही हांडी: Krishna Janmashtami Puja Festival
  • MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi
    MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi Biography

Structure in C program with example | Nested structure in C

Posted on December 17, 2021June 4, 2022 By GeekCer Education No Comments on Structure in C program with example | Nested structure in C
Structure in C program with example | Nested structure in C

Structure in C is a user-defined data type that the user defines according to his needs. These are collection of various types of data. Members of a structure can be of any type, including variables, pointers, arrays, and structures.

For example, you may want to retain and process student information such as name, class, marks, grade. For such purposes you can declare a structure “StudentInfo” with required fields.

struct StudentInfo
{
    char studName[20];
    float marks;
    char studClass[10];
    char grade;
} stud;

We declare a structure with the struct keyword. Structure, in terms of memory, takes up space in memory in proportion to its size.

Table of Contents

  • Syntax for a struct declaration in C
  • Accessing Structure Members in C
    • Structure Variables Definition and Declaration in C
    • Declaring Structure variables separately
    • Declaring Structure variables with structure definition
  • Nested Structure in C Programming
    • By using separate Structure
    • By using embedded Structure
  • Arrays of Structures in C
  • Pointers to structures in C language
  • Passing Structure to function in C Programming
  • Points to Remember About Structure in C

Syntax for a struct declaration in C

struct [structure-tag-name]
{
    // Structure member 1
    // Structure member 2
    // Structure member 3
    ...
} [structure variable(s)];
struct StudentDetails
{
	char name[20];
	char address[100];
	int age;
};

Accessing Structure Members in C

We access any member of the structure using the dot (.) operator and structure variables. We refer the dot (.) operator as the member access operator.

Structure Variables Definition and Declaration in C

Structure variables are similar to regular variables in that they allow us to quickly access members of a structure. We can declare variables of a struct during the definition of the struct or after definition.

Declaring Structure variables separately

We declare structure variables in the main() function using this technique.

struct StudentDetails
{ 
   // Structure definition
};
int main() 
{
	struct StudentDetails struct_var1, struct_var2;
	return 0;
}

Declaring Structure variables with structure definition

We declare the structure variable in this way at the end of the structure definition, immediately before the end of the structure.

struct StudentDetails
{ 
   // Structure definition

} struct_var1, struct_var2;

Nested Structure in C Programming

We can define a structure inside of another structure. This type of structure is called a nested structure. To put it another way, a nested structure is one that is a member of another structure. Structures can be nested mainly in the following two ways.

By using separate Structure

We create many structures in this form of nested structure and write the dependent structure as a structure member inside the primary structure.

struct Address  
{  
   char city[10];
   char state[10]; 
};  
struct Employee  
{     
   char name[20];  
   struct Address add;  
} emp1, emp2; 

By using embedded Structure

We declare the structure within the main structure as a structure member in this type of nested structure.

struct Employee  
{     
   char name[20];  
   struct Address  
   {  
	 char city[10];
	 char state[10]; 
   } add;
   
} emp1, emp2;  

Arrays of Structures in C

We can store multiple structures in an array. An array of structures is actually a collection of many structures. If you have a Student structure that requires you to keep 5 student records, you may use structure in this scenario.

struct Student
{    
	int roll;    
	char name[10];    
};    
int main(){    

	struct student stud[5];
}

Pointers to structures in C language

A pointer to a structure is defined as a pointer to the address of a memory block containing the structure. To put it another way, a pointer will contain the address of structure.

Click here to learn pointer in C programming

#include<stdio.h>
#include<conio.h>
struct Emp
{

    int id;
    float salary;
};
void main()
{
    struct Emp emp,*ptr;
    printf("Input values : \n");
    ptr = &emp;
    scanf("\n%d%f",&ptr->id, &ptr->salary);
    printf("\nEmployee Record");
    printf("\n%d\t%f",ptr->id, ptr->salary);
}
Output:
Input values : 
10001
50000

Employee Record
10001   50000.000000

Passing Structure to function in C Programming

Functions can take structures as parameters, and functions can return structures. We can pass a structure to a function either as a member or as a variable.

You can learn functions in c programming from this link.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct Website
{
  char type[30];
  char title[30];
};
void display(struct Website w);
void main()
{
   struct Website w;
   strcpy(w.type, "Educational Website");
   strcpy(w.title, "C Programming for Geeks");
   display(w);
}
void display(struct Website w)
{
	printf("%s", w.type);
	printf("\n%s", w.title);
}  
Output:
Educational Website
C Programming for Geeks

Points to Remember About Structure in C

  • Members of the structure are assigned memory locations in a sequential order.
  • You can define a structure of arrays or an array of structures, and you can use dot operator to refer the members.
  • A function can return a structure.
  • It is possible to modify the members of a structure by passing the structure as an argument.

Recommended Articles

  • Storage classes and its types
  • Pointers in c programming with examples
  • DBMS Concepts And Architecture Introduction
  • Class and Structure difference
  • What is the difference between internet and intranet?

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

C Language

Post navigation

Previous Post: Pointers in C Programming, Definition, Types & Example
Next Post: Storage classes in C with examples (auto, extern, register, static)

More Related Articles

Linked List in Data Structure and Algorithm, Types, Operations Linked List in Data Structure and Algorithm, Types, Operations C Language
Storage classes in C with examples (auto, extern, static, register) Storage classes in C with examples (auto, extern, register, static) C Language
File Handling in C with examples | File Operations in C File Handling in C with examples | File Operations in C C Language
Constants and Operators in C Language Constants and Operators in C Language C Language
String Array in C | What is an array of string | String Functions String Array in C | What is an array of string | String Functions C Language
Function in C Language | Recursive function with example Function in C Language | Recursive function with example C Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • C Home
  • Constants and Operators
  • Statements in C Language
  • Array in C
  • String Array in C
  • Functions in C
  • Pointers in C
  • Structure in C
  • Storage Classes
  • File Handling in C
  • Top C Programs
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved