STRUCTURE
A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.
A struct in the C programming language is a composite data type declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the same address.
CREATING A STRUCTURE
struct keyword is used to create a structure
Example:-
struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
DECLARE STRUCTURE VARIABLES
A structure variable can either be declared with a structure declaration or as a separate declaration like basic types.
struct Point
{
int x, y;
}p1;
struct Point
{
int x,y;
};
int main()
{
struct Point p1;
}
In C++, the struct keyword is optional before in the declaration of a variable. In C, it is mandatory.
ACCESSING STRUCTURE MEMBERS
Structure members are accessed using the dot (.) operator.
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
}
OUTPUT:-
x=20 , y=1
ARRAY IN STRUCTURES
An array of structures in C can be defined as the collection of multiple structures variables where each variable contains information about different entities. The array of structures in C are used to store information about multiple entities of different data types. The array of structures is also known as the collection of structures.
#include<iostream.h>
void main()
{
struct employee
{
int no;
float bal;
};
struct employee a[10];
int i;
for(i=0; i<=9; i++)
{
printf("enter amount number and balance");
scanf("%d%f", &a[i].no , &a[i].bal);
}
for(i=0; i<=9; i++)
printf("%d%f", a[i].no , a[i].bal);
}
0 Comments
Doubts? Please let our team know So that we can serve you better.