position: fixed; top: auto !important; margin-left: 112px;

Structure in C

Array in C does not allow to store data elements of a different kind whereas Structures allows storing data elements of a different kind. Structure in C is a user-defined data type which allows you to store data elements of a different kind.

Why Structure is used?

Suppose you want to store the information of student in a college, with attributes like Name, Roll No, Age, Gender. So, Name should be defined with a datatype "String", Age should be defined with a datatype "Integer". Here you clearly understand the difference between Arrays and Structures. In Arrays, you can only store elements of one type either Names of "String" Datatype or Ages of "Integer" Datatype whereas in Structures you can store both. That’s the magic of structures.


Defining a Structure

The following example shows how to define a Structure in C using struct keyword:

Syntax:-

struct [structure Name]
 {
  datatype variable;
  datatype variable;
  ...
  datatype variable;
 } [one or more structure variables];

"structure Name" is an "Optional" Name which is used to define a structure.

"datatype variable" is simply declaring a normal variable int a; float per;

"[one or more structure variables]" is an "Optional" Name which is used to define one or more structure variables.


Example :-

struct Student
 {
  char name[20]; 
  int   age;
 } student; 

How to Access Structure Members?

Dot Operator (.) also known as member access operator is used to access any C structure member.

Following example clears the concept :-

#include <stdio.h>
#include <string.h>

struct Students
{
    char name[20];
    int   age;
};

int main( )
{
    /* Declare firstStudent of type Students */
    struct Students firstStudent;
    /* Declare secondStudent of type Students */
    struct Students secondStudent;
    
    /* firstStudent specification */
    strcpy( firstStudent.name, "Jazzy");
    firstStudent.age = 23;
    
    /* secondStudent specification */
    strcpy( secondStudent.name, "Steven");
    secondStudent.age = 22;
    
    /* print firstStudent info */
    printf("First Student Name: %s\n",   firstStudent.name);
    printf("First Student age: %d\n", firstStudent.age);
    
    
    /* print secondStudent info */
    printf("Second Student Name : %s\n", secondStudent.name);
    printf("Second Student age : %d\n",secondStudent.age);
    
    return 0;
}

Output :-

First Student Name: Jazzy
First Student age: 23
Second Student Name: Steven
Second Student age: 22

How to pass Structure as Function Argument?

How do we pass a normal variable to a function?
e.g: add(5,3) , swap(6,5)
Similarly, we can pass structure variables to function .

Example :-

#include <stdio.h>
#include <string.h>

struct Students {
    char  name[50];
    int  age;
 };

/* function declaration */
void display( struct Students student );

int main( ) {
    
    struct Students firstStudent;       
    struct Students secondStudent;        
    
    /* Student 1 specification */
    strcpy( firstStudent.name, "Manish");
    firstStudent.age = 23;
    
    /* Student 2 specification */
    strcpy( secondStudent.name, "Nitesh");
    secondStudent.age = 24;
    
    /* print FirstStudent info */
    display( firstStudent );
    
    /* Print Student2 info */
    display( secondStudent );
    
    return 0;
}

void display( struct Students student ) {
    
    printf( "Name : %s\n", student.name);
    printf( "Age : %d\n", student.age);
}
Output :-
Name : Manish
Age : 23
Name: Nitesh
Age : 24

How to pass Structure with Pointer Arguments?

You can define pointers to structures in the same way as you define pointer to any other variable

struct Students *pointer_Struct;

Now, you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the &; operator before the structure's name as follows

pointer_Struct = &firstStudent;

To access the members of a structure using a pointer to that structure, you must use the operator as follows

Example :-

pointer_Struct->name;
#include <stdio.h>
#include <string.h>

struct Students {
    char  name[50];
    int  age;
 };

/* function declaration */
void display( struct Students *student );

int main( ) {
    
    struct Students firstStudent;        
    struct Students secondStudent;        
    
    /* Student 1 specification */
    strcpy( firstStudent.name, "Manish");
    firstStudent.age = 23;
    
    /* Student 2 specification */
    strcpy( secondStudent.name, "Nitesh");
    secondStudent.age = 24;
    
    /* print FirstStudent info */
    display( &firstStudent );
    
    /* Print Student2 info */
    display( &secondStudent );
    
    return 0;
}

void display( struct Students *student ) {
    
    printf( "Name : %s\n", student->name);
    printf( "Age : %d\n", student->age);
}

Output :-
Name : Manish
Age : 23
Name : Nitesh
Age : 24


Solve the Quiz of Article

1) What do you think Size of structure is size of the longest element in the structure?
Yes
No

2) What do you think elements of structure are always accessed using & operator?
Yes
No


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.