Learn Structures in C

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.

For example:

Ads code goes here

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
struct Person
{
    char name [50];
    int age;
    float salary;
};

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:

READ  10 Causes Why C is the Best Programming Language For Beginners

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

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 Comment

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