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

  • 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
  • Difference between Interface and Abstract class
    Difference between Interface and Abstract class Differences
  • Spring Boot Tutorial
    Spring Boot Tutorial Spring Boot
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Anupama TV serial and Cast name | Anuj kapadia in Anupama
    Anupama Serial Cast & Crew name | Anuj kapadia in Anupama News
  • Fundamental Rights of Indian Citizens
    Fundamental Rights of Indian Citizens | मौलिक अधिकार क्या हैं? General Knowledge
  • Loop in Java
    Loop in Java Java
  • Array in C programming | Character Array in C
    Array in C programming | Character Array in C C Language

Structure in C program with example | Nested structure in C

Posted on December 17, 2021April 28, 2022 By admin 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.

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

Statements in C Language | Types of statements in C Language Statements in C Language | Types of statements in C Language C Language
Programming with C Language Tutorial Programming with C Language Tutorial C Language
Linked List in Data Structure and Algorithm, Types, Operations Linked List in Data Structure and Algorithm, Types, Operations C Language
Top 100 C programs for geeks Top 100 C programs for geeks C Language
Array in C programming | Character Array in C Array in C programming | Character Array in C C Language
File Handling in C with examples | File Operations in C File Handling in C with examples | File Operations in C C Language

Related Posts

  • Statements in C Language | Types of statements in C Language
    Statements in C Language | Types of statements in C Language C Language
  • Programming with C Language Tutorial
    Programming with C Language Tutorial C Language
  • Linked List in Data Structure and Algorithm, Types, Operations
    Linked List in Data Structure and Algorithm, Types, Operations C Language
  • Top 100 C programs for geeks
    Top 100 C programs for geeks C Language
  • Array in C programming | Character Array in C
    Array in C programming | Character Array in C C Language
  • File Handling in C with examples | File Operations in C
    File Handling in C with examples | File Operations in C 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

Recent Posts

  • Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti
  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • 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
  • Difference between Interface and Abstract class
    Difference between Interface and Abstract class Differences
  • Spring Boot Tutorial
    Spring Boot Tutorial Spring Boot
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Anupama TV serial and Cast name | Anuj kapadia in Anupama
    Anupama Serial Cast & Crew name | Anuj kapadia in Anupama News
  • Fundamental Rights of Indian Citizens
    Fundamental Rights of Indian Citizens | मौलिक अधिकार क्या हैं? General Knowledge
  • Loop in Java
    Loop in Java Java
  • Array in C programming | Character Array in C
    Array in C programming | Character Array in C C Language
  • 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