C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Structure in Structure

C Programming

In this tutorial we will learn to handle structure in a structure in C programming language.

We have already learned how to create and access members of a structures in the structure tutorial.

Now, lets add a structure inside a structure and learn how to work with it.

In the following example we have a student structure. Inside it we have another structure address to hold the address detail.

// student structure having address structure inside
struct student {
  char name[255];
  char id[20];
    
  // address of the student
  struct {
    char line1[255];
    char line2[255];
    char city[255];
    char state[100];
    char country[255];
    char pincode[20];
  } address;
};

In the above code snippet we can see that we have an address structure inside the student structure.

We already know by now that to access any member of a structure variable we use the . operator.

So, if we want to access the name member of the student structure we have to write the following.

std.name

Similarly if we want to access the city member of the address structure which is inside the student structure, we have to write the following.

std.address.city

In the following code we will take student detail from the user and then print the output.

#include <stdio.h>

int main(void) {
  // student structure having address structure inside
  struct student {
    char name[255];
    char id[20];
    
    // address of the student
    struct {
     char line1[255];
     char line2[255];
     char city[255];
     char state[100];
     char country[255];
     char pincode[20];
    } address;
  };
  
  // create structure variable
  struct student std;
  
  // take student data
  printf("Enter student detail:\n");
  printf("Name: ");
  gets(std.name);
  printf("ID: ");
  gets(std.id);
  printf("Address Line1: ");
  gets(std.address.line1);
  printf("Address Line2: ");
  gets(std.address.line2);
  printf("City: ");
  gets(std.address.city);
  printf("State: ");
  gets(std.address.state);
  printf("Country: ");
  gets(std.address.country);
  printf("Pincode: ");
  scanf("%s", std.address.pincode);
  
  // display result
  printf("Student Detail:\n");
  printf("Name: %s\n", std.name);
  printf("ID: %s\n", std.id);
  printf("\nAddress of the student:\n");
  printf("%s\n", std.address.line1);
  printf("%s\n", std.address.line2);
  printf("%s, %s %s\n", std.address.city, std.address.state, std.address.country);
  printf("%s\n", std.address.pincode);
  
  return 0;
}

Output:

Enter student detail:
Name: Yusuf Shakeel
ID: s01
Address Line1: House #123
Address Line2: Awesome Street, 1024th Main Road       
City: Bangalore
State: KA
Country: India
Pincode: 560001

Student Detail:
Name: Yusuf Shakeel
ID: s01
Address of the student:
House #123
Awesome Street, 1024th Main Road
Bangalore, KA India
560001

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT