C langugae : Structures

The word structure is used in two senses. One is the sense "structure plan," which is what we just discussed. The structure plan tells the compiler how to represent the data, but it doesn't make the computer allocate space for the data. The next step is to create a "structure variable," the second sense of the word. The line in the program that causes a structure variable to be created is this.Structure includes the following topics
• Structure definitions
• Initializing structures
• The dot operator (.)
• Structure assignments
• Nested structures
Structure definitions
A structure is a collection of one or more variable types.Each element in an array must be the same data type, and you must refer to the entire array by its name. Each element (called a member) in a structure can be a different data type.
A structure is like a "superarray," in which one element can be char, the next element float, and the next an int array. You can access the individual elements of an array by using a subscript. How do you access individual members of a structure? Use a dot (.), the structure member operator. For example, library.value is the value portion of library. You can use library.value exactly as you would use any other float variable. Similarly, you can use library.title exactly as you would use a char array. Therefore, the program uses expressions such as
struct book library;
Structures can follow two approaches:
1. Construct individual arrays, one for storing names, another for storing prices and still another for storing number of pages.
2. Use a structure variable.
A structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together. A structure declaration forms a template that can be used to create structure objects (that is, instances of a structure). The variables that make up the structure are called members. (Structure members are also commonly referred to as elements or fields).
Memory allocation for a structure.
A structure can be used in a program only if memory is allocated to it. The structure definition informs the compiler about the type and the number of data members in the structures. However, it does not allocate any memory for the structure. To use a structure in a program efficiently, a structure variable needs to be declared. The syntax for declaring a structure variable is structure_name structure_variable;
When a structure variable is declared, the C++ compiler automatically allocates sufficient memory for accommodating all the members of the structure variable. For example, when the structure variable record1 of structure sales_record is declared, the compiler allocates 9 bytes (1 byte for grade, 2 bytes for item_code, 2 bytes for item_quantity and 4 bytes for item_rate) of memory space to record1.

Note that each structure variable contains its own copy of structure members and all the structure members occupy adjacent memory locations.
Initializing a Structure
To initialize a structure object explicitly when you define it, you must use an initialization list: this is a comma-separated list of initializers, or initial values for the individual structure members, enclosed in braces. The initializers are associated with the members in the order of their declarations: the first initializer is associated with the first member, the second initializer goes with the second member, and so forth. Of course, each initializer must have a type that matches (or can be implicitly converted into) the type of the corresponding member.
Accessing Members of a Structure
Here the dot is an operator which selects a member from a structure.
st_rec.name
Selecting a member from a structure pointer : -
st_ptr -> name
To initialize a structure (any storage class for ANSI C, but excluding automatic variables for pre-ANSI C), you use a syntax similar to that used for arrays.An example:
#include < stdio.h >
int main(void)
{
struct {
int a;
int b;
} x, y;
x.a = 10;
y = x; /* assign one structure to another */
printf("%d", y.a);
return 0;
}
After the assignment, y.a will contain the value 10.
Arrays of Structures
Structures are often arrayed. To declare an array of structures, you must first define a structure and then declare an array variable of that type. For example, to declare a 100-element array of structures of type addr defined earlier, write
struct addr addr_list[100];
Identifying Members of an Array of Structures
To identify members of an array of structures, you apply the same rule used for individual structures: Follow the structure name with the dot operator and then with the member name. Here's an example:

Declaring an array of structures is like declaring any other kind of array. Here's an example:
#include < stdio.h >
#include < string.h >
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record[2];
// 1st student's record
record[0].id=1;
strcpy(record[0].name,"Anuj");
record[0].percentage=86.5;
// 2nd student's record
record[1].id=2;
strcpy(record[1].name,"Babloo");
record[1].percentage=90.5;
// 3rd student's record
record[2].id=3;
strcpy(record[2].name,"kumar Anuj ");
record[2].percentage=81.5;
for(i=0;i<3;i++)
{
printf(" Records of Student : %d \n", i+1);
printf("Id is : %d\n", record[i].id);
printf("Name is : %s\n", record[i].name);
printf("Percentage is : %f\n\n", record[i].percentage);
}
return 0;
}
Records of STUDENT : 1
Id is: 1
Name is: Anuj
Percentage is: 86.5
Records of STUDENT : 2
Id is: 2
Name is: Babloo
Percentage is: 90.5
Records of STUDENT : 3
Id is: 3
Name is: kumar Anuj
Percentage is: 81.5
C structure using pointer
There are at least three reasons why having pointers to structures is a good idea. First, just as pointers to arrays are easier to manipulate (in a sorting problem, say) than the arrays themselves, pointers to structures are often easier to manipulate than structures themselves. Second, in some older implementations, a structure can't be passed as an argument to a function, but a pointer to a structure can. Third, many wondrous data representations use structures containing pointers to other structures.
1.Program for C structure using pointer
2.Program to copy a structure in C
1.Example program for C structure using pointer:: -
#include < stdio.h >
#include < string.h >
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record1={ 1, "Anuj", 90.5};
struct student *ptr;
ptr=&record1;
printf("Records of STUDENT1: \n");
printf("Id is: %d \n", ptr->id);
printf("Name is : %s \n", ptr->name);
printf("Percentage is: %f \n\n", ptr->percentate);
return 0;
}
Records of STUDENT1:
Id is: 1
Name is: Anuj
Percentage is: 90.500000
2. Example program to copy a structure in C:
There are many methods to copy one structure to another structure in C.
1.We can copy using direct assignment of one structure to another structure
2.We can use C inbuilt function “memcpy()”
3.We can copy by individual structure members.
#include < stdio.h >
#include < string.h >
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record1={1,"Anuj",90.5};
struct student record2, *record3, *ptr1, record4;
printf("Records of Student1- record1 structure \n");
printf(" Id : %d\n Name : %s\n percentage : %f\n", record1.id, record1.name,record1.percentage);
// 1st method to copy whole structure to another structure
record2=record1;
printf("\n Records of STUDENT1 -Direct copy from" \ "record\n");
printf(" Id : %d \n Name : %s\n Percentage : %f\n",record2.id, record2.name,recore2.percentage);
// 2nd method to copy using memcpy function
prt1=&record1;
memcpy(record3, prt1,sizeof (record1));
printf("\n Records of Student1- copied form record1" \ "using memcpy \n");
printf(" Id : %d \n Name : %s\n Percetage : %f\n", record3->id, record3->name,record3->percentage);
//3rd method to copy by individual members
printf("\n Records of Student1 - copied individual" \ "members from record1\n" );
record4.id=record1.id;
strcpy(record4.name,record1.name);
record4.percentage=record1.percentage;
printf(" Id: %d\n Name : %s\n Percentage : %f \n", record4.id, record4.name, record4.percentage);
return 0;
}
Output:
Records of STUDENT1 - record1 structure
Id : 1
Name : Anuj
Percentage : 90.500000
Records of STUDENT1 – Direct copy from record1
Id : 1
Name : Anuj
Percentage : 90.500000
Records of STUDENT1 – copied from record1 using memcpy
Id : 1
Name : Anuj
Percentage : 90.500000


Free Web Hosting