Monday, January 13, 2014

C - Structure. What is structure in C Language?

A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities.

The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with point here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.

The variables named in a structure are called members. A structure member or tag and an ordinary variable can have the same name without conflict, since they can always be distinguished by context. Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.

Uses of C structures:

    1. 1. C Structures can be used to store huge data. Structures act as a database.
    2. 2. C Structures can be used to send data to the printer.
    3. 3. C Structures can interact with keyboard and mouse to store the data.
    4. 4. C Structures can be used in drawing and floppy formatting.
    5. 5. C Structures can be used to clear output screen contents.
    6. 6. C Structures can be used to check computer’s memory size etc.

Difference between C variable, C array and C structure:

    • 1. A normal C variable can hold only one data of one data type at a time.
    • 2. An array can hold group of data of same data type.
    • 3. A structure can hold group of data of different data types
    • 4. Data types can be int, char, float, double and long double etc. 
Datatype

C variable

C array

C structure

Syntax Example Syntax Example Syntax Example
int int a a = 20 int a[3] a[0] = 10
a[1] = 20
a[2] = 30
a[3] = ‘\0′
struct student
{
int a;
char b[10];
}
a = 10
b = “Hello”
char char b b=’Z’ char b[10] b=”Hello”

Below table explains following concepts in C structure.

    1. 1. How to declare a C structure?
    2. 2. How to initialize a C structure?
    3. 3. How to access the members of a C structure?
Type Using normal variable Using pointer variabe
Syntax struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Example struct student
{
int  mark;
char name[10];
float average;
};
struct student
{
int  mark;
char name[10];
float average;
};
Declaring structure variable struct student report; struct student *report, rep;
Initializing structure variable struct student report = {100, “Mani”, 99.5}; struct student rep = {100, “Mani”, 99.5};  report = &rep;
Accessing structure members report.mark
report.name
report.average
report  -> mark
report -> name
report -> average

 Example program for C structure:

           This program is used to store and access “id, name and percentage” for one student. We can also store and access these data for many students using array of structures. You can check “C – Array of Structures“ to know how to store and access these data for many students.

#include <stdio.h>
#include <string.h>

struct student
{
           int id;
           char name[20];
           float percentage;
};

int main()
{
           struct student record = {0}; //Initializing to null

           record.id=1;
           strcpy(record.name, "Caroline");
           record.percentage = 86.5;

           printf(" Id is: %d \n", record.id);
           printf(" Name is: %s \n", record.name);
           printf(" Percentage is: %f \n", record.percentage);
           return 0;
}

Output:

Id is: 1
Name is: Caroline
Percentage is: 86.500000

Example program – Another way of declaring C structure:

           In this program, structure variable “record” is declared while declaring structure itself. In above structure example program, structure variable “struct student record” is declared inside main function which is after declaring structure.
#include <stdio.h>
#include <string.h>

struct student
{
            int id;
            char name[20];
            float percentage;
} record;

int main()
{

            record.id=1;
            strcpy(record.name, "Raju");
            record.percentage = 86.5;

            printf(" Id is: %d \n", record.id);
            printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
            return 0;
}

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

C structure declaration in separate header file:

In above structure programs, C structure is declared in main source file. Instead of declaring C structure in main source file, we can have this structure declaration in another file called “header file” and we can include that header file in main source file as shown below.
Header file name – structure.h

Before compiling and executing below C program, create a file named “structure.h” and declare the below structure.

struct student
{
int id;
char name[20];
float percentage;
} record;
Main file name – structure.c:

           In this program, above created header file is included in “structure.c” source file as #include “Structure.h”. So, the structure declared in “structure.h” file can be used in “structure.c” source file.
// File name - structure.c
#include <stdio.h>
#include <string.h>
#include "structure.h"   /* header file where C structure is
                            declared */

int main()
{

   record.id=1;
   strcpy(record.name, "Raju");
   record.percentage = 86.5;

   printf(" Id is: %d \n", record.id);
   printf(" Name is: %s \n", record.name);
   printf(" Percentage is: %f \n", record.percentage);
   return 0;
}

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000


0 comments:

Post a Comment