JIYIK CN >

Current Location:Home > Learning > PROGRAM > C语言 >

Structure arrays in C

Author:JIYIK Last Updated:2025/04/17 Views:

This tutorial explains how to create a structure array in C language, which is a collection of multiple structure variables, each of which contains information about a different entity.


Structure arrays in C

An array is a sequential collection of elements of the same data type, and a structure is a user-defined data type. The declaration of an array of a structure is the same as an array of a primitive data type, but using the data type that the structure has its elements.

Consider an Studentexample of a structure called , shown below.

struct Student
{
    int rollNumber;
    char studentName[10];
    float percentage;
};

We can declare an array of structures as follows.

struct Student studentRecord[5];

Here, studentRecordit is an array of 5 elements, each of which is of type Studentstruct. Individual elements []are accessed using index notation and members (.)are accessed using the dot operator.

studentRecord[0]Points to the element of the array 0th, studentRecord[1]points to the element of the array 1th.

Similarly,

  • studentRecord[0].rollNumberRefers to the member in the 0th element of the array rollNumber.
  • studentRecord[0].studentNameRefers to the member in the 0th element of the array studentName.
  • studentRecord[0].percentageRefers to the member of the 0th element of the array percentage.

structThe complete program to declare an array in C language is as follows.

#include<stdio.h>
#include<string.h>

struct Student
{
    int rollNumber;
    char studentName[10];
    float percentage;
};

int main(void)
{
    int counter;
    struct Student studentRecord[5];

    printf("Enter Records of 5 students");

    for(counter=0; counter<5; counter++)
    {
        printf("\nEnter Roll Number:");
        scanf("%d",&studentRecord[counter].rollNumber);
        printf("\nEnter Name:");
        scanf("%s",&studentRecord[counter].studentName);
        printf("\nEnter percentage:");
        scanf("%f",&studentRecord[counter].percentage);

    }

    printf("\nStudent Information List:");

    for(counter=0; counter<5; counter++)
    {
        printf("\nRoll Number:%d\t Name:%s\t Percentage :%f\n",
               studentRecord[counter].rollNumber,studentRecord[counter].studentName, studentRecord[counter].percentage);
    }
    return 0;
}

Output:

Enter Record of 5 students
Enter Roll number:1
Enter Name: John
Enter percentage: 78

Enter Roll number:2
Enter Name: Nick
Enter percentage: 84

Enter Roll number:3
Enter Name: Jenny
Enter percentage: 56
  
Enter Roll number:4
Enter Name: Jack
Enter percentage: 77
   
Enter Roll number:5
Enter Name: Peter
Enter percentage: 76
    
   Student Information List
   Roll Number: 1		Name: John		percentage:78.000000
   Roll Number: 2		Name: Nick		percentage:84.000000
   Roll Number: 3		Name: Jenny		percentage:56.000000
   Roll Number: 4		Name: Jack		percentage:77.000000
   Roll Number: 5		Name: Peter		percentage:76.000000
       

malloc()Create an structarray using the C function

In C language, there is another way to make structarrays, you can use malloc()the function to allocate memory for structthe array. This is called dynamic memory allocation.

malloc()(Memory Allocation) function is used to dynamically allocate a large block of memory of a specified size. This function returns a voidpointer of type .

The returned pointer can be cast to any form of pointer. It initializes each block with default garbage values.

malloc()The syntax of the function is as follows.

ptrVariable = (cast-type*) malloc(byte-size)

The complete program to dynamically create an array of structures is as follows.

#include<stdio.h>
int main(int argc, char** argv)
{
    typedef struct
    {
        char* firstName;
        char* lastName;
        int rollNumber;

    } STUDENT;

    int numStudents=2;
    int x;
    STUDENT* students = malloc(numStudents * sizeof *students);
   
    for (x = 0; x < numStudents; x++)
    {
        students[x].firstName=(char*)malloc(sizeof(char*));
       
        printf("Enter first name :");
        scanf("%s",students[x].firstName);
        students[x].lastName=(char*)malloc(sizeof(char*));
        printf("Enter last name :");
        scanf("%s",students[x].lastName);
        printf("Enter roll number  :");
        scanf("%d",&students[x].rollNumber);

    }

    for (x = 0; x < numStudents; x++)
        printf("First Name: %s, Last Name: %s, Roll number: %d\n",students[x].firstName,students[x].lastName,students[x].rollNumber);

    return (0);
}

Output:

Enter first name:John
Enter last name: Williams
Enter roll number:1

Enter first name:Jenny
Enter last name: Thomas
Enter roll number:2
    
First Name: John	Last Name: Williams		Roll Number:1
First Name: Jenny	Last Name: Thomas		Roll Number:2

Previous: None

Next:Initialize array to 0 in C

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Flushing stdout output stream in C

Publish Date:2025/04/17 Views:200 Category:C语言

stdout This article will demonstrate various methods on how to flush an output stream in C language . Use function in C language fflush to refresh stdout output stream I/O The C standard library provides a stdio buffered version of the I/O

Using the feof function in C language

Publish Date:2025/04/17 Views:87 Category:C语言

feof This article will introduce several ways to use functions in C language . Use feof the function to check the end-of-file indicator on a file stream in C language feof The function is part of the C standard input/output library and is d

Use the C language setenv function to access environment variables

Publish Date:2025/04/17 Views:122 Category:C语言

setenv This article will introduce several methods of using functions to export environment variables in C language . setenv Use function to export environment variables in C language Every program running on a Unix-base system has an envir

How to get the size of an array in C

Publish Date:2025/04/17 Views:62 Category:C语言

This tutorial explains how to determine the length of an array in C. sizeof() The operator is used to get the size/length of an array. sizeof() Operator determines the size of an array in C sizeof() The operator is a compile time unary oper

Initialize array to 0 in C

Publish Date:2025/04/17 Views:133 Category:C语言

This article explains how to initialize an array to 0 in C language. The declaration of an array in C language is as follows. char ZEROARRAY[ 1024 ]; It becomes all zeros at runtime in the global scope. If it is a local array, there is a si

在C中将整数转换为字符

Publish Date:2024/01/03 Views:135 Category:C语言

本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。

在 C 语言中使用 typedef enum

Publish Date:2023/05/07 Views:372 Category:C语言

本文介绍了如何在 C 语言中使用 typedef enum。使用 enum 在 C 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。

C 语言中的静态变量

Publish Date:2023/05/07 Views:170 Category:C语言

本文介绍了如何在 C 语言中使用静态变量。在 C 语言中使用 static 变量在函数调用之间保存变量值

C 语言中生成随机数

Publish Date:2023/05/07 Views:159 Category:C语言

本文演示了如何在 C 语言中生成随机数。使用 rand 和 srand 函数在 C 语言中生成随机数

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial