As you are aware that arrays store
elements of similar type. Contrary to this, structures are objects that contain
more than one item.
Definition:
- A structure contains a number of data types grouped together.
- These data types may or may not be of the same type.
In this article, you will learn following fundamental aspects of structures:
(a) declaration of a structure
(b) accessing of structure elements
(c) Initializing values to
structure element(s)
(d) how structure elements are
stored in memory
(a)
declaration of a structure
The general form of a structure
declaration statement is given below:
struct structure_name{
datatype structure_element 1 ;
datatype structure_element 2 ;
datatype structure_element 3 ;
......
......
} structure_variable(s);
Points
to remember about above declaration:
- The first line contains the struct keyword, then the optional structure_name:struct structure_name {
- The structure_name can be used to create a copy of the structure. An opening brace follows the structure_name (or the struct keyword, if the structure_name is not used). This brace signals to the compiler that the next lines are member definitions. Each member definition consists of a variable type and a name. The members can be any valid variable type, including arrays, structures, and unions.
- Following the last member name is a closing brace and the optional structure_variable, as follows:} structure_ variable(s) ;
- The closing brace in the structure type declaration must be followed by a semicolon.
- A structure type declaration does not reserve any space in memory. All a structure declaration does is, it defines the ‘form’ of the structure.
When using the structure_ variable
and the structure_name, you can choose any of the following:
- If a structure_ variable is not specified and a structure _name is specified, the structure is being defined but not declared.
- If a structure_ variable is specified and a structure _name is not specified, the structure is being declared but not defined.
- If a structure_ variable and a structure _name are provided, the structure is being both defined and declared.
- If neither a structure_ variable nor a structure _name is provided, a compile-time error will result.
If you want to initialize the
structure, you must have a structure_ variable because
it signals the compiler that this
is a declaration. The structure_ variable is also necessary if you want to
refer to the structure.
After the structure_variable are
optional initializers:
{initializer_values};
Example:
struct student
{
int rollno;
int marks;
} stu1;
(b).Accessing
Structure Elements
In arrays we can access individual
elements of an array using a subscript. Structures use a different scheme. They
use a dot (.) operator.
Note that before the dot there must
always be a structure variable and after the dot there must always be a
structure element.
Syntax:
structure_variable.structure_element;
If the structure_element is also a
structure ,the structure_element name is followed by a period and its structure_element
name:
structure_variable.memberstructure. structure_element
Example:
stu1.rollno;
In
this example, I will show you how to declare a structure, assign values and
access the elements of structure.
#include<stdio.h>
struct student
{
int rollno;
int marks;
} stu1,stu2;
int main(){
return 0;
}
(c).Initializing
values to structure element(s)
Unlike standard
variables, the syntax for initialising structure variables is different.The structure elements are accessed using the dot
notation.
stu1. rollno = 6532;
stu1. marks = 89;
or by simply listing the element's value
inside curly braces, with each value separated by a comma.
stu1 = {6532,89};
A structure variable can
be assigned to another providing that
the structures have the same number of members and that the members are of the same data type.
Example : stu2=stu1;
(d).How Structure
Elements are Stored
Whatever be the elements of a
structure, they are always stored in contiguous memory locations.
Example:
#include <stdio.h>
struct student{
char *name;
int rollno;
int marks;
}stu1;
int main()
{
/*Assigning the values of each struct element here*/
stu1.name = "Steve";
stu1.rollno = 1234;
stu1.marks = 30;
/* Displaying the values of struct element */
printf("Student Name is: %s", stu1.name);
printf("\nStudent roll no is: %d", stu1.rollno);
printf("\nStudent marks is: %d", stu1.marks);
return 0;
}
Please comment if you find anything incorrect, or you want to improve the topic discussed above.
No comments:
Post a Comment