×

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still, there is a programming need to store multiple items of different data types, for example, when collecting data from students whose names are of the character data type. Next is the roll number of the numeric data type. Structures are used to store multiple elements of different data types.

Structure

The structure is a user-defined data type. With the help of structures, we can define a data type that holds more than one element of different types. The structure can contain "n" elements. This means any number of elements. Now let's look at the syntax.

Syntax

struct structurename
{
datatype ele1;
datatype ele2;
.
.
.
.
};

We should always start with the keyword "struct" and add the struct name that the programmer can want. Next, we need to open the square bracket declaration. Finally, close the brackets after declaring all the elements with the data type. Adding a semicolon at the end is important.

Syntax Example

struct Customer
{
int customerid;
char name[20];
};
main()
{
int a;
Struct Customer c[25];
}

When we declare a variable in the main function, we know that we need to add the data type and then the variable name. For structures, the data type name is "struct structure name". This means that we are a "struct Customer" here. Next, we need to add the variable name "c" here, and memory will be allocated for the variable. Structure variables always contain a base address. This refers only to the data type, but there is no pointer here since it is an internal pointer value. A structural variable called "c" is allocated 24 bytes of memory [20]. Each character occupies 1 byte of memory. Characters mean 20 bytes for 20 characters, and integer variables are 4 bytes on 64-bit systems for 24 bytes. We have saved the information so far. Let's move to the access part.

Accessing Structure

 We can access the structure simply by entering the dot ".". Operators such as "c.customerid" and "c.name".  Firstly, we need to understand how to declare the structure's size.

Structure size

 We can use the keyword "size" to find the size and sizeof (variable name). We can also use sizeof (struct strcutname). Check out the examples below to get a quick idea. 

Array Of Structures

Example in C Programming

#include<stdio.h>
struct Customer
{
int customerid;
char name[20];
};
void main()
{
struct Customer c;
printf("size of Customer structure is %lu  \n",sizeof(c));
printf("size of Customer is %lu ",sizeof(struct Customer));


}

Output

Array Of Structures

Array

Arrays are simply a collection of multiple elements, but they must be of the same data type. An array of C can be defined as a way to combine multiple entities of similar type into a larger group. These entities or elements can be user-defined data types such as ints, floats, chars, double data types, or structures. However, all elements must have the same data type to be stored together in a single array. The items are organized from left to right, with the leftmost index being 0 and the rightmost index being (n-1).

Collection of characters, numbers are some examples of Arrays.

Array Of Structures

An array of structures is a simple array where each element is a structure of the same type.

struct Customer
{
int customerid;
char name[20];
};
struct student s1,s2

This is the normal format where we declare variables. If we want to add Sixty members, then declare 60 variables like s1,s2,s3, and many, finally, s60. Declaring like this is time taking when a huge number of variables are there, and the program length will increase, making the code more complicated.

So, it is better to take an array of these elements. They represent 60 structure objects, which is the main reason for using the Array of Structures.

Applications Of Array Of Structures

When we need to store many people's data where the required details are the same for all, it means when we collect 60 members' data, we need customer id and names. The values may differ from customer to customer, but the required details titles are the same.

Memory Allocation

Each array element is a structure object, and each object has all the details. A base address is assigned to the array. The allocation of 3 elements is like this. To access the customer id of the second customer, we need to say “c[2].customerid” .If we want to access the name, then s[2].name.

Array Of Structures

Example in C

#include <stdio.h>
struct Customer
{
int customerid;
char name[20];
}s[3];
int main()
{  
int i;
for(i;i<3;i++)
{
    printf("Enter the values of id and name \n");
scanf("%d \n %s",&s[i].customerid,&s[i].name);


}
printf("The entered details of all theree customers: \n");


for(int i;i<3;i++)
{
printf("\n %d \n %s \n",s[i].customerid,s[i].name);
}




    return 0;
}

Output

Array Of Structures

To display all details, we need to use for loop. Finally, we added 3 customers data and print them in the output.


Related Topics

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.