Tutorial references

c
c++
c#

Tutorial references

chapters

Structure

Structure

Structure is a user define data-type in C language. Structure holds different type elements.

Each element of structure is called member.

Structure syntax:

struct structure_name{
data-type member-variable-1;
data-type member-variable-2;
data-type member-variable-3;
...........
...........
};

struct is a keyword.

structure_name is name of structure. This name is given by programmers.

Example:

Example

struct student{
char stName[100];
char stMob[15];
char stAddress[300];
};

In above example "student" is the name of structure. stName, stMob, stAddress are member of structure.

Declaring Structure Variable

There are two ways to declare structure variable.

  (1.) with structure body.

  (2.) In main() function.

1st-way(with structure body) Syntax:

struct structure_name{
data-type member-variable-1;
data-type member-variable-2;
data-type member-variable-3;
...........
...........
}struct_variable1, sruct_variable2,......;

Example:

struct student{
char stName[100];
char stMob[15];
char stAddress[300];
}st1,st2;

In above example st1 and st2 are structure type variable.

2nd-way(in main() function) Syntax:

struct structure-name variable;

Example

struct student st1,st2;

Accessing Member of Structure

There are two ways to access structure members:

   (1)By . (member or dot operator)

   (2)By -> (structure pointer operator)

Example

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

struct student{
char stName[100];
char stMob[15];
char stAddress[300];
}st1,st2;

void main(){
clrscr();
puts("Enter name of student:");
gets(st1.stName);
puts("Enter mobile number of student:");
gets(st1.stMob);
puts("Enter address of student:");
gets(st1.stAddress);
printf("\n******************************\n");
puts("Enter name of student:");
gets(st2.stName);
puts("Enter mobile number of student:");
gets(st2.stMob);
puts("Enter address of student:");
gets(st2.stAddress);
printf("\n========student details==========");
printf("\nName of student: ");
printf(st1.stName);
printf("\nStudent mobile number: ");
printf(st1.stMob);
printf("\nAddress of student: ");
printf(st1.stAddress);
printf("\n--------------------------------");
printf("\nName of student:");
printf(st2.stName);
printf("\nStudent mobile number:");
printf(st2.stMob);
printf("\nAddress of student:");
printf(st2.stAddress);
getch();
}

Run program

Example

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

struct student{
char stName[100];
char stMob[15];
char stAddress[300];
};

void main(){
struct student st1,st2;
clrscr();
puts("Enter name of student:");
gets(st1.stName);
puts("Enter mobile number of student:");
gets(st1.stMob);
puts("Enter address of student:");
gets(st1.stAddress);
printf("\n******************************\n");
puts("Enter name of student:");
gets(st2.stName);
puts("Enter mobile number of student:");
gets(st2.stMob);
puts("Enter address of student:");
gets(st2.stAddress);
printf("\n========student details==========");
printf("\nName of student: ");
printf(st1.stName);
printf("\nStudent mobile number: ");
printf(st1.stMob);
printf("\nAddress of student: ");
printf(st1.stAddress);
printf("\n--------------------------------");
printf("\nName of student:");
printf(st2.stName);
printf("\nStudent mobile number:");
printf(st2.stMob);
printf("\nAddress of student:");
printf(st2.stAddress);
getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018