Structure

 

Structures

Why uses structure?

we need to keep more than one attribute for an entity in program. It is capable of having various properties from various data types.

As for example students may have a name (string), roll number (int), and marks (float). let we have to store some properties related to a Student like a Name, Class, and div etc. We have one method to create a character array to store the Name, integer variable for Class, and character variable for div.

 What Is a Structure?

The structure is a user-defined data structure that is used to bind two or more data types or data structures together.

For storing the details of a student, we can create a structure for a student that has the following data types-

 a character array for storing name,

 an integer for storing roll number, and

 a character for storing div, etc.

Structures don't take up any space in the memory unless and until we define some variables for it. When we define its variables, they take up some memory space which depends upon the type of the data member .

Defining a Structure

To define a structure, use the struct keyword statement. 

Syntax-:

struct name_of_the_structure

{

    data_type member1;

    data_type member2;

    ...

    data_type memberN;

}; varibles;

Eg.

struct Store

   // structure definition

   char storeName

   int totalBooks;

   char storeLicense[20];

} storeA, storeB;   // structure variables

 

How to Declare structure variable?

To make it simple to access a structure member, we can create a variable for the structure. Structure variables can be declared in one of two ways:

1.  By using the struct keyword in the main () method.

2.  By declaring a variable when the structure is defined.

Eg.

struct employee 

{   int id; 

    char name[50]; 

    float salary; 

}e1,e2;

Initialize Structure Members

Eg.

struct Student student1 = {"Student_name" , 1, 'A'};

 

Accessing members of the structure

There are two ways to access structure members:

1.  By . (member or dot operator)

2.  By -> (structure pointer operator)

Structure members can be accessed and assigned values in a number of ways. Structure members have no meaning individually without the structure. the member name must be linked with the structure variable using a dot . operator also called period or member access operator.

Example 1

 struct Student

{

    char name[25];

    int age;

    char branch[10];

     char gender;

}S1, S2;

Example2

#include<stdio.h>

#include<string.h>

struct Student

{

    char name[25];

    int age;

    char branch[10];

        char gender;

};

 int main ()

{

    struct Student s1;

       /*

        s1 is a variable of Student type and

        age is a member of Student

    */

    s1.age = 18;

    /*

        using string function to add name

    */

    strcpy(s1.name, "Viraaj");

    /*

        displaying the stored values

    */

    printf("Name of Student 1: %s\n", s1.name);

    printf("Age of Student 1: %d\n", s1.age);

       return 0;

}

Nested Structure

The nested word means placed or stored one inside the other. As the structure in C is a user-defined data type so while creating a structure, we can define another structure as its data member, which leads to a structure with another structure inside it. 

#include <stdio.h>

 // creating a structure

struct Student

{

   char name[50];

   int class;

   // defining a nested structure

   struct Address

   {

       char city[50];

       int pincode;    

   }addr;

} student = {"Student_one", 5, "city_1", 1234};

// variable defined with structure

 // creating another structure

struct Subject

{

   char name[50];

   char book[50];

};

 // creating another structure with one nested structure

struct Teacher

{

   char name[50];

      // added already defined structure

   struct Subject subject;

};

 int main()

{

   // declaring variable for Teacher structure

   struct Teacher teacher;

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

   scanf("%s",teacher.subject.name);

   scanf("%s",teacher.subject.book);

     // printing values for teacher variable

printf("Name : %s\: %s\n",teacher.name,teacher.sub);

     // printing values for student variable

printf("Name : %s\n City: %s",student.name,student.city);

   return 0;

}

 Array of Structure

We can also declare an array of structure variables. in which each element of the array will represent a structure variable.

 Example : struct employee emp[5];

 #include<stdio.h>

 struct Employee

{

    char ename[10];

    int sal;

};

struct Employee emp[5];

int i, j;

void ask()

{

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

    {

        printf("\nEnter %dst Employee record:\n", i+1);

        printf("\nEmployee name:\t");

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

        printf("\nEnter Salary:\t");

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

    }

    printf("\nDisplaying Employee record:\n");

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

    {

        printf("\nEmployee name is %s", emp[i].ename);

        printf("\nSlary is %d", emp[i].sal);

    }

}

void main()

{

    ask();

}

 Passing Structures to Function 

We can pass a structure as a function argument just like we pass any other variable or an array as a function argument.

You can pass a structure as a function argument in the same way as you pass any other variable or pointer.

#include<stdio.h>

 struct Student

{

    char name[10];

    int roll;

};

 void show(struct Student st);

 void main()

{

    struct Student std;

    printf("\nEnter Student record:\n");

    printf("\nStudent name:\t");

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

    printf("\nEnter Student rollno.:\t");

    scanf("%d", &std.roll);

    show(std);

}

 void show(struct Student st)

{

    printf("\nstudent name is %s", st.name);

    printf("\nroll is %d", st.roll);

}

 What Is a Structure Pointer?

A pointer is a variable that stores the address of another variable. As a structure consists of some data types or data structures for which memory is allocated to the structure variable, we can use a structure pointer to store the address of that memory. A structure pointer is essentially a pointer to a structure variable.

 use the arrow operator (->) to access the structure member using a pointer.

Example

 Here, var is the structure variable of struct1 and ptr is the pointer variable. The ptr is storing the address of the var. In the case of a pointer to a structure, the members can be accessed using the arrow (->) operator.

The following example illustrates the pointer to a structure .

#include<stdio.h>

struct myStruct

{

   int x, y;

};  

int main()

{

   struct myStruct var1 = {1, 2};  

   // var2 is a pointer to structure var1.

   struct myStruct *var2 = &var1;  

   // Accessing data members of myStruct using a structure pointer.

   printf("%d %d", var2->x, var2->y);

   return 0;

}

 

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Problems Classification

  Problems Classification Problem classification is the process of assigning a level, based on predefined criteria. Problem classification h...