Skip to main content

Structure In C Program #DOTC_Mdb

Structure in C Program.

 

Hello Everyone,

Today we will discuss about Structure in C Program . Before starting please go through my previous blogs in archive folder for your reference.

dheeraj60.blogspot.com

Let’s Discuss about Structure in C : As we know in Array we define type of variable that can hold several data types of the same types. Whereas Structure is basically used to represent a record, in other word we can say structure is another user defined data type available in C that allows to combined data items of different kinds.

Assume we need to store the data of students like student name, age, address, enrolment number etc. One way of doing this would be creating a different variable for each attribute, however when you need to store the data of multiple students then in that case, we need to create these several variables again for each student. This is bit complex but we can solve this problem easily by using structure. We can create a structure that has members for name, age, address and enrolment number and then we can create the variables of this structure for each student. We can use this by using structure in C with examples for better understanding.

Note : Struct Keyword is use to create structure in C . The struct statement defines a new data type, with more than one member. Defining structure by using struct is as below :

struct [structure tag] {

 

   datatype  memberdefinition;

   datatype member definition;

   ...

   datatype member definition;

};

 

Here in above Syntax  structure tag is optional and each member definition is a normal variable definition, such as int ,char , float etc.

Understand this by below statement for student attributes in Structure for above syntax:

struct Students {

   char  name[20];

   int  age[10];

   char  address[50];

   int   enrolment_number;

} students;   

 

To  access data members of a structure using a struct variable we use as below :

var_name.member1_name;

var_name.member2_name;

To assign value to structure members we use below method .

1.     By using Dot(.)  -- var_name.memeber1_name = value;

2.     We can assign all members in one statement as below method

 struct struct_name var_name =

{value for memeber1, value for memeber2 …}

 

Let’s take one simple example of Student details by using structure in one Program to understand this in better way.

Program is as below:

#include <stdio.h>

#include <stdlib.h>

struct StudentDetails{

    char *student_name;

    int age;

    char *address;

    int enrollment_number;

};

int main()

{

       struct StudentDetails student;

 

     student.student_name = "Ashish";

     student.age = 21;

     student.address = "DELHI";

     student.enrollment_number=10;

 

     printf("Student name is: %s", student.student_name);

     printf("\nStudent age is: %d", student.age);

     printf("\nStudent Address is: %s", student.address);

     printf("\nStudent enrollment number is: %d", student.enrollment_number);

 

    return 0;

}

 

Here in above program we create a structure  studentDetails with different members with several data types . and through  command   struct StudentDetails student;  student is the variable of structure StudentDetails and now we can Assigning the values of each struct member here by using student variable and finally through printf function  we are displaying the values of struct members in program.

 

Below is the output of above program in compiler code-block.

 

 

Here we can see the output of student details by using structure in C . So through Structure we can define multiple members as variable and can access and display it accordingly in program.

 

We can use Structures as Function Arguments : We can pass Structure as a function argument in the same way we can pass any variable or pointer . Let understand this with below example/ Program.

#include <stdio.h>

#include <stdlib.h>

 

struct Students {

      char  student_name[20];

      char  address[50];

      int   age;

      int   enrollment_number;

};

 

/* function declaration */

void printstudents( struct Students student );

 

int main( ) {

 

   struct Students students1;        /* Declare students1 of type student */

   struct Students students2;        /* Declare students2 of type student */

 

   /* students 1 specification */

   strcpy( students1.student_name, "Ashish");

   strcpy( students1.address, "Delhi Katwaria Sarai");

   students1.age = 21;

   students1.enrollment_number=100;

   /* students 2 specification */

   strcpy( students2.student_name, "ABCD");

   strcpy( students2.address,"Bangalore");

   students2.age = 30;

   students2.enrollment_number=101;

 

   /* print students1 info */

   printstudent( students1 );

 

   /* Print students2 info */

   printstudents( students2 );

  

  

 

   return 0;

}

 

void printstudents( struct Students student ) {

 

   printf( "Student name is : %s\n", student.student_name);

   printf( "Student Address is : %s\n", student.address);

   printf( "Student Age is : %d\n", student.age);

   printf( "Student enrollment number is : %d\n", student.enrollment_number);

}

 

 Here we display the value two times by declaring type of structure in 2 parts as students 1 and students 2 . In code i explained all the syntax in comment section so that you can understand the code properly .Only thing we need to understand  strcpy which i not discussed in previous blog

Important  :The strcpy() is a function basically used to copies the string pointed by source  to the destination. That’s why we used strcpy to both types.

 

 Above Program will Display as below once compiled .

Student Name is : Ashish

Student Address is : Delhi

Student Age is : 21

Student enrolment number is : 100

Student Name is : ABCD

Student Address is : Bangalore

Student Age is : 30

Student enrolment number is : 101

 

 

Hope you are able to understand the Structure in C , we can also use Pointer in Structure . Benefits of Structure is that it allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only. Please go through this blog carefully and try to write some different program by using arrays and let me know if you have any doubt . In next Blog i will come up with Pointers uses in Function and C function pointer.

 

Thanks.


Comments

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...

Aggregate Functions and Group By Clause in Oracle with Examples #Diksha Oracle Training Centre

  Aggregate Functions and Group By Clause Oracle with Examples.   Hello Everyone, Today I will   discuss about   Aggregate function in SQL by using Group By Clause and different clauses with some examples . Please go tjrough my previous blogs in Archive folder for classification of SQL, Commands and   SQL joins for your understanding.   Aggregate Functions Allows us to perform a calculation on a set of values to return a single value . We can use Group by Clause to group the result-set by one or more columns. Also we can use Having clause to restrict or filter the data as per our requirement. Note: Whenever we use Aggregate function in SQL we can’t able to use where condition. To restrict or filter the record we need to use having clause instead of Where. Below is the most commonly used Aggregate function in SQL.   MAX : Max function   used to get the maximum values in a set of values. COUNT : This function used to count rows in ...