Structures in C: Learn Quick In Just 5 Minutes

Introduction to Structures in C

This is a very important concept while Learning C Programming. Pointers and arrays are inextricably connected. In addition, in many cases, the pointers and arrays are interchangeable.

For example, by using either pointer arithmetic or array style indexing, a pointer pointing to the beginning of an array may access the array Consider the program which follows.

What is Structure in C?

Structure is user define datatype, using structure we can define a datatype that holds more than one element of different datatypes.

In short, Structure is a collection of the element of different datatypes.

How to define structure?

For example:

You want to store some information about a person: his/her name, citizenship number, and salary. You can easily create different variables name, citNo, salary to store this information separately.

However, in the future, you would want to store information about multiple persons. Now, you’d need to create different variables for each information per person: name1, citNo1, salary1, name2, citNo2, salary2, etc.

Basic Syntax for Structure

{

data-type    ele 1;

data-type     ele 2;

See also  Functions in C: Complete Guide in Just 5 Minutes

.

.

.

Data-type    ele n;

};

Example:
struct Person
{
    char name [50];
    int age;
    float salary;
};

How to use an Array and Structure?

Use of Array:

int arr[5]= {10,20,30,40,50};

char srt[5]= “four”;

array only stores homogenous types of elements, but using array we can’t store employee dataset or student dataset.

Array only stores the same kind of data type like int, float, char, etc.

Ex:- arr[5]={a,b,c,d,e};

Use of Structure:

Structure must be defined first in their data format, which can be used later to store several structure variables.

Consider a student database consisting of stud-name, stud-age, stud-weight, stud-phno, we can define a structure to hold this info like.

struct stud-details

{

Char name[20];

Int age;

float weight;

longint phno;

};

The keyword struct holds four details namely name, age, weight, ph-no. These are called structure elements. Each element can be a different data-type.

Here stud-details are called structure tags. Actually, it is describing a format, i.e template to represent the information.

name         array of 20 char

age             int

weight       float

ph-no         long int

Things you must remember when you are using structure:

  • The template will be ended by a semi-colon(;).
  • After assigning each member corresponding data-type there will be a semi-colon(;), as they all are independent statements.
  • The tag name such as stud-details can be used to declare structure variables further in the program.
  • The minimum size of the structure is one byte and the maximum is the sum of all member variable’s size.
  • We can’t assign an empty structure in c.

Some basic difference between structure and array:

ArrayStructure
1)      Array is a collection of the homogenous data type.1)      Structure refers to a heterogeneous data type.
2)      Array uses “[   ]” for element access.2)      Structure uses “.” For element access.
3)      Array object installation is not possible.3)      Structure object installation is possible.
4)      Array is the primary datatype.4)      Structure is user-defined datatype.
5)      Array data usually access using an index.5)      Structure elements are accessed using the operator.
6)      To access the array element we need less time.6)      To access the structure element we need more time.
See also  10 Causes Why C is the Best Programming Language For Beginners

How to create Structures with Array?

Structures can be mostly used with arrays because we have to access data in the form of partitions.

Sample program to work with Structure with Array

#include <stdio.h>
#include <string.h>
struct Distance
{
    int feet;
    float inch;
} dist1, dist2, sum;

int main()
{
    printf("1st distance\n");
    printf("Enter feet: ");
    scanf("%d", &dist1.feet);

    printf("Enter inch: ");
    scanf("%f", &dist1.inch);
    printf("2nd distance\n");

    printf("Enter feet: ");
    scanf("%d", &dist2.feet);

    printf("Enter inch: ");
    scanf("%f", &dist2.inch);

    // adding feet
    sum.feet = dist1.feet + dist2.feet;
    // adding inches
    sum.inch = dist1.inch + dist2.inch;

    // changing to feet if inch is greater than 12
    while (sum.inch >= 12) 
    {
        ++sum.feet;
        sum.inch = sum.inch - 12;
    }

    printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch);
    return 0;
}
Output on screen:

1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances = 15′-5.7″

Further visit: Recursion In C: All-inclusive Just In 5 Minutes

How to pass Structures into Functions?

This is a very important concept while Learning C Programming. A structure variable can be passed to a function in a similar way as a normal argument. Consider this example:

#include <stdio.h>
#include <string.h>
struct student 
{
            int id;
            char name[20];
            float percentage;
};
 
void func(struct student record);
 
int main() 
{
            struct student record;
 
            record.id=1;
            strcpy(record.name, "Raju");
            record.percentage = 86.5;
 
            func(record);
            return 0;
}
 
void func(struct student record)
{
            printf(" Id is: %d \n", record.id);
            printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
}
Output on screen:

Id is: 56747
Name is: Animesh
Percentage is: 86.500000
Practice question on C

 

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *