• Have Any Question?
  • contact@eitworld.com

C Structure





  • A structure is a collection one or more different data-type, grouped together under a single name.
  • Structure are basically used where we have to store complete information for any object like ‘student’ so all the information of a student like student-name, age, class, phone number etc. can be stored in a structure type variable.
  • When we want to insert different types of values then we have to use structure.
  • We cannot make any empty structure in C.

Why Structure: In C programming, array holds the same type of data items but when we want to store or we want to insert different types of values then we have to use structure.

Declaration of Structure

    • Structure is declared using the ‘struct’ keyword.
    • The basic syntax for a declaration of a structure is as follows:
struct struct_name

{

Data-type variable1;

Data-type variable2;

…………………………..

…………………………..

Data-type variable n;

};

    • We can define a struct named as Student as follows:
struct Student

{

char name[10];

int age;

char branch[4];

char city[10];

char telephone[20];

};

    • You can create a structure type variable by using either of the following syntax:

 

struct Student

{

char name[10];

int age;

char branch[4];

char city[10];

char telephone[20];

} s1;

………………..OR…………………….. struct Student

{

char name[10];

int age;

char branch[4];

char city[10];

char telephone[20];

} ;

struct Student s1;

    • Datamember of structure can be accessed using ‘.’ Or access operator.

For example:

student.name;

student.age;

    • We can initialize the structure elements as the following statement:
struct student s1={“Radha”,21,”C.S”,”Delhi”,”9778123545″};

    • We can directly assign values to the members as follows:
s1.name=”Radha”;

s1.age=21;

    • Here is an example of which is sufficient to understand the concept of structure:
Example

 

#include<stdio.h>

#include<conio.h>

struct student //declaration of structure

{

char name[10];

int age;

char branch[4];

char city[10];

char telephone[20];

};

int main()

{

//declaration and initialization of structure type variable

struct student s1={“Radha”,21,”C.S”,”Delhi”,”9778123545″};

printf(“Student’s Information:”);

printf(“nName:%s”,s1.name);

printf(“nAge:%d”,s1.age);

printf(“nbranch:%s”,s1.branch);

printf(“ncity:%s”,s1.city);

printf(“ntelephone:%s”,s1.telephone);

getch();

return 0;

}

    • The output of the above program is as follows:
Output

 

Student’s Information: Name: Radha

Age: 21

branch: C.S

city: Delhi

telephone: 9778123545

Difference between Array and Structure:

Array Structure
Array holds same type of data values It can hold different types of data value.
In array, data is assessed via index In structure, data is assessed via operator.
It allocates static memory It allocates dynamic memory.

Array of structure

    • We can also create an array of structure. Array of structure can be declared as follows:
struct Student

{

char name[10];

int age;

char branch[4];

char city[10];

char telephone[20];

} s1[3];

……………….OR………………

struct Student

{

char name[10];

int age;

char branch[4];

char city[10];

char telephone[20];

} ;

struct Student s1[3];

    • Here is an example which shows the concept of Array of structure:
Example

 

#include<stdio.h>

#include<conio.h>

struct student //declaration of structure

{

char name[10];

int age;

char branch[4];

char city[10];

char telephone[20];

};

int main()

{

//declaration and initialization of structure type variable

struct student s1[3]={{“Radha”,21,”C.S”,”Delhi”,”9778123545″},

{“Ram”,20,”E.C”,”Agra”,”9887123456″},

{“Radhika”,22,”I.T”,”Jaipur”,”8004123345″}};

int i;

for(i=0;i<3;i++)

{

printf(“nStudent %d Information:”,i+1);

printf(“nName:%s”,s1[i].name);

printf(“nAge:%d”,s1[i].age);

printf(“nbranch:%s”,s1[i].branch);

printf(“ncity:%s”,s1[i].city);

printf(“ntelephone:%s”,s1[i].telephone);

}

getch();

return 0;

}

    • The output of the above program is as follows:
Output

 

Student 1 Information: Name: Radha

Age: 21

branch: C.S

city: Delhi

telephone: 9778123545 Student 2 Information: Name: Ram

Age: 20

branch: E.C

city: Agra

telephone: 9887123456

Student 3 Information: Name: Radhika

Age: 22

branch: I.T

city: Jaipur

telephone: 8004123345

Pointer To Structure

 

    • As we know pointer is a variable that holds the address of another variable. In the same way we can also define the pointer to structure that is used to store the address of the structure type variable.
    • Let us assume a structure as follows:
struct book

{

char bookname[15];

char authorname[15];

int pages;

};

    • We can declare a structure pointer for above structure as follows:
struct book *ptr;

    • You can assign the address of the structure variable to structure pointer by using & operator as follows:
struct book b;

struct book *ptr;

ptr=& b;

    • To access member of the structure using structure pointer you have to follow the given syntax:
ptr->bookname;

ptr->authorname;

ptr->pages

    • Here is an example-program of the structure pointer:
Example

 

#include<stdio.h>

#include<conio.h>

struct book

{

char bookname[15];

char authorname[15];

int pages;

};

void main()

{

struct book b={“C-Programming”,”J.C.Mishra”,1000};

struct book *ptr;

ptr=&b;

printf(“nPrint value of data-members of structure using pointer:”);

printf(“nbookname=%s,authorname=%s,pages=%d”,ptr->bookname,ptr->authorname,ptr->pages);

getch();

}

 

    • The output of the above program is as follows:
Output

 

Print value of data-members of structure using pointer:

bookname= C-Programming,authorname= J.C.Mishra,pages=1000

Structure and Functions

 

    • Like variable you can also pass the structure to a function as argument.
    • Here is a simple program of using structure as function argument:
Example

 

#include<stdio.h>

#include<conio.h>

void display(struct book *b1);

struct book

{

char bookname[15];

char authorname[15];

int pages;

};

void main()

{

struct book b={“C-Programming”,”J.C.Mishra”,1000};

display(&b);

getch();

}

void display (struct book *b1)

{

prinf(“Structure as function argument:”);

printf(“nbookname=%s,authorname=%s,pages=%d”,b1->bookname,b1->authorname,b1->pages);

}

    • The output of the above program is as follows:

Output

Structure as function argument: bookname= C-Programming,authorname= J.C.Mishra,pages=1000



Real Time Web Analytics