Structure
Definition
- Structure and Union is a data structure/variables that store large no. of data items in contiguous memory location whose individual elements may differ in their datatypes.
- A structure is a user-defined data type available in C that allows to combining data items of different kinds.
- A structure is a collection of related data members of same or different data types variables under a single name at one place in memory.
Characteristics
- Structures are used to represent a record.
- A C structure is a convenient way of grouping several pieces of related information together just like a record.
- A single structure might contain integer elements,floating–point elements, character elements etc together.Pointers, arrays, function, union and other structures can also be included as elements within a structure.
- It is a collection of different variables under a single name and each may have different datatype at one place.
- Structures members can be processed in the same manner as ordinary/simple variables of the same data type.
- Each individual variable/data element of a structure is called a member/structure element of the structure.They can hold any number of variables as per need. we can make arrays of structures for large no. of data items and thus structures are ideally useful for creating databases in C for storing data permanently.The individual members can be ordinary variables, pointers, arrays, functions, union or other structures. The member names within a particular structure must be distinct from one another, though a member name can
be same as the name of a variable defined outside of the structure. - The members of a structure variable can be assigned with initial/static values in much the same manner as the elements of an array.The initial values must appear in the order in which they will be assigned to their corresponding structure members, enclosed in braces and separated by commas. The general form/syntax is –
storage-class struct tag variable = { value1, value 2,——-, value m};
example –
{
int roll_no;
float marks;
struct dob d1;
};
static struct student st = { “ Rajiv”, 241, 87.26, 17-12-1911};
- Structure and Union can be passed through function and they can also be returned from functions.
- If s1 and s2 are two structure variables having the same composition then It is possible to copy the values of s1 to s2 simply by using s2=s1;
- Unlike class, struct is created on stack. So, it is faster to instantiate (and destroy) a struct than a class.
Syntax
- Declaration of Structure :
- To declare a structure we use a keyword called struct followed by the structure name or structure tag and within the braces there may be the list of the structure’s member variables.
- Syntax :
struct structure-tag/name
{
datatype variablename1;
datatype variablename2;
dataype variablename3;
...
...
};
------- OR -------
struct tag
{
member 1;
member 2;
------------
-------------
member m;
}
-
- Example
struct student
{
int rollno;
char stuname[20];
char course[20];
float coursefee;
};
-
- To access structure elements easily for reading and writing data from structure, we must use at least one structure variables/structure instances. when structure variable is declared then memory will be allocated.The amount of memory allocated will be the sum of all the data members (datatype) which take part in the formation of structure.To create structure variables : –
struct student
{
int rollno;
char stuname[20];
char course[20];
float coursefee;
}x,y;
--------- OR ---------
struct student
{
int rollno;
char stuname[20];
char course[20];
float coursefee;
};
struct student x,y;
Here, x and y are two structure variables.
- To Access Structure Members :
- Structure members are accessed using the structure member operator (.), also called the dot operator, between the structure name and the member name.This period (.) symbol is an operator, and it is a member of the highest precedence in group, and its associativity is left-to-right.
-
The syntax for accessing the member of the structure is: –
{
int rollno;
char stuname[20];
char course[20];
float coursefee;
};
struct student x,y;
variable.member.submember.
-
-
Array of Structure is an array in which each element is a structure. for example –
-
{
int rollno;
char stuname[20];
char course[20];
float coursefee;
}x[10];
by writing –
Advantage
- Structure helps to construct a complex data type which is more meaningful in many places.
- A structure is a convenient way of grouping several pieces of related information together.
- Structure provides the functionality of creating a user defined data type which is very useful for handling large number of data in row and column manner.
- structure also provide the functionality of implementing in many data structures such as linked list which is used to implement trees, graphs etc
Disadvantage
- It uses comparatively more memory during processing than union.
- All structure elements are public in nature i.e. no security of data elements.
- All the structure elements are located contiguously in the memory of computer.
Union
Definition
- A union is a special data type available in C that allows storing different data types in the same memory location.
0 Comments