Structures in C Programming


 What is a structure?

  •  A structure is a user defined data type in C/C++. 
  • A structure creates a data type that can be used to group items of possibly different data types into a single type.
  • In C, there are cases where we need to store multiple attributes of an entity.
  • It is not necessary that an entity has all the information of one type only.
  • It can have different attributes of different data types. For example, an entity Student may have its name (string), roll number (int), marks (float)


The struct statement is follows:

struct [structure tag] {
   member definition;
   member definition;
    ...
     member definition;
} [one or more structure variables]; 

Structures in c-language collection of elements with different data types

Steps:

1)Define a structure. 

2)Declare structure variable. 

3)Initialize the member of structure. 

4)Accessing the members of a structure.

Syntax:

we use  Tagname 

Struct Tagname 

{
           datatype variable name
           datatype variable name
            .
            .

};

using the Tag name are declare the structure variable . Tag name is optional. 

For normal variable---->stores value,

For pointer variable ----->stores address,

For structure variable ---->access to all the members. 

If we write a Tag name we can declare n number of structure variables anywhere in the program. 

If we are not used the Tagname we have to declare all the structure variable in the defination. 

For example:

(1) WITH USE IF TAGNAME :

Struct Student
{
      int id; 
      char name [20];
      float Percent;
};
 struct student s1;   // To declare Structure variable it can access the all the members. 
 struct student s1 s2; // To declare two variable

(2) WITH OUT USE OF TAGNAME:

struct {
       int id;
       char name [20];
       float percent;
}s1,s2;

  • we can write the structures in before (or) after the main function
  • all the Structure variable can be defined Inside the main function (or)both structure and declaration in inside the main function.

Intialization:

There are two types of intialization

1)Run time intialization using scanf. 

2)Compile time intialization using the assignment operator  "=". 

1) Compile time intialization: //input given the program itself

Struct Student s1= [20, "kumar", 75.1};
Struct student s2= [30, "xyz", 78.6};


To access the members in structure.
we use the dot operator '.'  to access the members 
Eg: To access the id=> s1. id
       The name =>s1.name
       The percent=>s1. percent
(OR)
s1.id = 20

2) Run time initialization : //input given at the run time 

Scanf ("%d %s %f ",&s1.id, s1.name,&s1.percent); 

If we write id =>It is not take because it is member of a Structure. 

Sample program 1:-

struct student 

{

int id;

       char name [20];

        float percent;

};

main () {

struct student s1= [10,"ABC",78.5);

Struct Student s2; 

printf("enter the details of s2");

scanf(%d,%s,%f,&s2.id,s2.name,&s2.percent);

printf("Details of student 1");

printf("student id=%d\tstudent name =%s\t student percent=%f",s1.id,s1.name,s1.Percent);

printf("Details of student2");

printf("s2.id=%d \ts2.name=%s\ts2.percnt=%f",s2.id,s2.name,s2.percent);

}

Memory occupance:

int a => 2 Bytes. Both 317 32= 26 bytes

        s1→id ->2 byte 

name[20]->20 bytes

Percent->4 bytes

Total=> 26 bytes

Nested structures:

In this we see Nested structures it means declaring structure valiable as a member structure.

student having id, name, dob -day month Year.

Case(1):-

struct dob

{

int day;

int month;

int year;

};

struct student

{

int id;

char name[20];

struct dob d1;

};

main () {

struct student s1;

print f("enter id");

scanf("%d",s1.id);

printf("Name is: ");

scanf("%s", s1.name");

printf("enter date of birth");

scanf("%d%d%d",&s1.d1.day,&s1.d1.month,&s1.d1.year);

printf( "ID=%d",sl.id);

printf("Name=%s",sl.name);

Printf("DOB=%d-%d-%d",s1.d1.day,s1.d1.month,s1.d1.year);

Array of structures:

* Thee arrays is used in structures as we want to read the to students data in require to Structure variables. To avoid these we use these arrays.

* while creating or declaring a structure variable there we use always to get move no. of variables.

Eg:Read Some to Employee details. having name, eid, salary.

Note:-int salary can store atmost 32767 salary if move it gives negative we use long gate.

Array is a collection of structure vairable 

struct employee

{

char name [20];

int eid;

int sal;

}

main ()

{

struct employee emp [10]; //emp is array holds to structre variable.

Printf("employee Details");

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

printf ("enter employee name");

scanf("s",emp [i].name);

printf ("enter employee id");

scanf("%d",&emp[i].eid);

printf("enter employee salary"); 

scanf("d",&emp[i].sal);

}

for(i=o; i<10; 1++)

{

  printf ("employee Name is %s", emp[i].name);

  printf("employee id is %d",emp[i].eid);

 Printsf("employe salary is %d",emp[i].sal);

}

Structures through pointers:

Steps:-

1)Access the members using Pointers.

2)For this we have to define a structure & declare a structure variable & Pointer variable

3)Initalize the pointer variable with structure & using pointers we have access the members structure.

we have two operators

1)* →value Operator

2)&->address operator

In pointers:

int a; → declare variable

int *p; → declare pointer variable

P = &a ->intialize the variable to pointer variable

procedure:

1)define structure

2)declare structure variable

3)declare pointer variable

4)intialize 

5)access members of a structure

Eg:-

struct student

{

        int rollno;

char name[20];

float percen;

}s1;

*If it integer pointer used to hold integer variable .we have to declare with a integer datatype. It is also some for remaining datatypes.

struct student s1;

struct student * Ptr; // Ptr is a pointer variable

ptr=&s1;

for Normal variable:-

s1.rollno

s1.name

s1.percen

for Accessing the structure Variable we use using pointer Variable we use "->"arrow operator.

ptr->rollno

ptr->name 

ptr->рercen

Sample Program:

struct student 

{

        int roll no ; 

        char name [20];

        float percen;

}

main ()

{

struct student S1, *ptr;

ptr=&s1;

printf("Enter roll no: ")

scanf("%d",&s1.rollno);

printf ("enter name");

scanf("%s", sl.name);

printf("Enter percentage);

scanf("%f", &s1.percen);

printf("rollno=%d",ptr->rollno);

printf("names=%s", ptr->name); 

printf("percentage = %f", Ptr->percen);

}

Note: More examples will be covered in Problems on C page.

Similarly you can also learn the arraysunions and pointers concepts in C programming Language.

If you have any doubts or you want us to create some more concepts or if you can't solve any problems please give it in the form of your comments.

If you like the content we had given and understood it well then give your valuable rating for this page and comment your experience in the comment Box for improvement.



Categories:

1 comment: