Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

malloc memory for an array of structure in c

Open
#30 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Stale
Tech stack
c
Domain
documentation

Research direction

Start with the issue's linked DelftStack article and supplied C example. Determine how this repository organizes programming tips, then add the malloc-based array-of-struct example with its explanation and output in the appropriate format. Done means the tip is included and readable in the repository.

Written by the indexing model from the issue text.

Description

Create an Array of struct Using the malloc() Function in C
There is another way to make an array of struct in C. The memory can be allocated using the malloc() function for an array of struct. This is called dynamic memory allocation.
The malloc() (memory allocation) function is used to dynamically allocate a single block of memory with the specified size. This function returns a pointer of type void.

The returned pointer can be cast into a pointer of any form. It initializes each block with the default garbage value.

The syntax of malloc() function is as below:

ptrVariable = (cast-type*) malloc(byte-size)
The complete program to create an array of struct dynamically is as below.

#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

Dominant language
Python
Stars
5
Forks
2
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from davideuler/programming-tips

All issues in davideuler/programming-tips

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.