Monday, 20 February 2012

What is a structure? | C Programing

A structure is a collection of pre-defined data types to create a user-defined data type. Let us say we need to create records of students. Each student has three fields:
int roll_number;
char name[30];
int total_marks;
This concept would be particularly useful in grouping data types. You could declare a structure student as:
struct student 
{
int roll_number;
char name[30];
int total_marks;
}
 student1, student2;
The above snippet of code would declare a structure by name student and it initializes two objects student1, student2. Now these objects and their fields could be accessed by saying student1.roll_number for accesing roll number field of student1 object, similarly student2.name for accesing name field of student2 object.