C struct{Structure} | Concept with example | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Friday, May 29, 2020

C struct{Structure} | Concept with example | My CS Tutorial


STRUCTURES IN C | C STRUCTURES | DEFINE,INITIALIZE WITH EXAMPLE | NESTED STRUCTURE IN C | ARRAY OF STRUCTURE | STRUCTURE IN C PROGRAMMING LANGUAGE | PASSING STRUCTURE

Structures :


 Structure is a user-defined data type in C which allows you to combine
different data types to store a particular type of record.

 Structure helps to construct a complex data type in more meaningful way.

 Structure is used to represent a record. Suppose you want to store record
of Student which consists of student name, address, roll number and age.
You can define a structure to hold this information.

Difference between Array and Structures

The array is used to store collection of similar data types while structure can store
collection of any type of data.

Structure in c | my cs tutorial


Defining a structure

struct keyword is used to define a structure. struct define a new data type which
is a collection of different type of data.

Syntax :

struct structure_name
{
 data_type member1;
 data_type member2;
 .
 .
 data_type memeber;
};

Example of Structure:-

struct Book
{
char name[15];
int price;
int pages;
};

Here the struct Book declares a structure to hold the details of book which consists
of three data fields, namely name, price and pages. These fields are called
structure elements or members.

STRUCTURE IN C | C STRUCTURE | DEFINE,INITIALIZE WITH EXAMPLE | NESTED STRUCTURE IN C | ARRAY OF STRUCTURE | STRUCTURE IN C PROGRAMMING LANGUAGE

Each member can have different data type,like in this case, name is of char type
and price is of int type etc. Book is the name of the structure and is called structure
tag.

Declaring Structure Variables

It is possible to declare variables of a structure, after the structure is defined.
Structure variable declaration is similar to the declaration of variables of any other
data types. Structure variables can be declared in following two ways.

 Declaring Structure variables separately:-

struct Student
{
char[20] name;
int age;
int rollno;
} ;
struct Student S1 , S2; //declaring variables of Student

 Declaring Structure Variables with Structure definition:-

struct Student
{
char name[20];
int age;
int rollno;
} S1, S2 ;

Here S1 and S2 are variables of structure Student. However this approach is not
much recommended.

Size of Structure 

The size of structure is the total size of variable declared in the structure.
In the above example the size of structure is

Char name[20] = 20 bytes
Int age= 2 bytes
Int rollno = 2 bytes
Total size of structure is = 20+2+2= 24 bytes

We use sizeof(structure variable) keyword to find the size of structure

Accessing Structure Members


Structure members can be accessed and assigned values in number of ways.
Structure member has no meaning independently. In order to assign a value to a
structure member, the member name must be linked with the structure variable
using dot . operator also called period or member access operator.

struct Book
{
char name[15];
int price;
int pages;
} b1 , b2 ;
b1.price=200; //b1 is variable of Book type and price is member of Book
We can also use scanf() to give values to structure members through terminal.
scanf(" %s ", b1.name);
scanf(" %d ", &b1.price);

Structure Initialization


Like any other data type, structure variable can also be initialized at compile time.

struct Patient
{
float height;
int weight;
int age;
};
struct Patient p1 = { 180.75 , 73, 23 }; //initialization
or
struct patient p1;
p1.height = 180.75; //initialization of each member separately
p1.weight = 73;
p1.age = 23;

Array of Structure


We can also declare an array of structure. Each element of the array representing
a structure variable.

Example : struct employee emp[5];

The below code define an array emp of size 5 elements. Each element of array
emp is of type employee

#include<stdio.h>
#include<conio.h>
struct employee
{
char ename[10];
int sal;
};
struct employee emp[5];
int i,j;
void ask()
{
for(i=0;i<3;i++)
{
 printf("\nEnter %dst employee record\n",i+1);
 printf("\nEmployee name\t");
 scanf("%s",emp[i].ename);
 printf("\nEnter employee salary\t");
 scanf("%d",&emp[i].sal);
}
printf("\nDisplaying Employee record\n");
for(i=0;i<3;i++)
{
 printf("\nEmployee name is %s",emp[i].ename);
 printf("\nSlary is %d",emp[i].sal);
}
}
void main()
{
clrscr();
ask();
getch();
}


C – Nested Structure (Nesting of Structure)


 Nested structure in C is nothing but structure within structure. One
structure can be declared inside other structure as we declare structure
members inside a structure.

 The structure variables can be a normal structure variable or a pointer
variable to access the data

Example :
struct student
{
char[30] name;
int age;
 struct address // Nesting of Structure
 {
 char[50] locality;
 char[50] city;
 int pincode;
 };
};

C – Passing structure to function (Important)


 A structure can be passed to any function from main function or from any
sub function.

 Structure definition will be available within the function only.

 It won’t be available to other functions unless it is passed to those
functions by value or by address (reference).

 Else, we have to declare structure variable as global variable. That means,
structure variable should be declared outside the main function. So, this
structure will be visible to all the functions in a C program.

Example to demonstrate

#include <stdio.h>
#include <string.h>
struct student
{
 int id;
char name[20];
 float percentage;
};
void func(struct student record);
int main()
{
 struct student record;
 record.id=1;
 strcpy(record.name, "Raju");
 record.percentage = 86.5;
 func(record); // Passing structure record in function
 return 0;
}
void func(struct student record)
{
 printf(" Id is: %d \n", record.id);
 printf(" Name is: %s \n", record.name);
 printf(" Percentage is: %f \n", record.percentage);
}
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.500000

STRUCTURE IN C | C STRUCTURE | DEFINE,INITIALIZE WITH EXAMPLE | NESTED STRUCTURE IN C | ARRAY OF STRUCTURE | STRUCTURE IN C PROGRAMMING LANGUAGE


Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN


No comments:

Post a Comment