0

How to access the array elements after allocate the memory.

I cont able to allocate a memory inside a structure how do perform that:

How do allocate dynamic memory for a array inside a structure in C and How to access it

#include<stdio.h>
#include<stdlib.h>
struct student{
    int *arr = (int*) malloc(10 * sizeof(int));
    int reg;
};


void main()
{
    struct student *ptr = (struct student*) malloc(sizeof(struct student));
    ptr->reg = 10;
    ptr->arr[0] = 100;
    printf("register no : %d\n",ptr->reg);
    printf("register no : %d\n",ptr->arr[0]);
    return ;
}
1
  • Please don't cast results from malloc() and any void*. It is pointless, error prone and adds only clutter. Commented Oct 7, 2022 at 17:09

4 Answers 4

1

You can't give a default value for struct members.

Once you have an instance of struct student, then you can allocate memory to the arr member.

#include <stdio.h>
#include <stdlib.h>

struct student{
    int *arr;
    int reg;
};


int main()
{
    struct student *ptr = malloc(sizeof(struct student));
    ptr->reg = 10;
    ptr->arr = malloc(10 * sizeof(int))
    ptr->arr[0] = 100;
    printf("register no : %d\n",ptr->reg);
    printf("register no : %d\n",ptr->arr[0]);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

One common pattern is to create a function that does the allocation and freeing of the struct (error handling not included) for you:

#include <stdlib.h> // malloc(), free()

struct student {
    int *arr;
    int reg;
};

// ...

struct student *student_allocate(int reg_n) {
    struct student *student = malloc(sizeof(* student));

    student->arr = malloc(sizeof(int) * reg_n);
    student->reg = reg_n;

    return student;
}

void student_free(struct student *student) {
    free(student->arr);
    free(student);
}

// ...

int main() {
    struct student *student = student_allocate(10);

    // do something with student

    student_free(student);
}

Comments

1

In addition to @dbush's answer, you can also reorder your struct to make the pointer as a 1-sized array in the last field and allocate the whole stuff at once:

struct student {
    int reg; // First fields are the general info
    // any other fixed-size fields
    int arr[1]; // LAST field is the array
};

Then,sizeof(struct student) is the size of the "general info" plus the size of an int, so you can allocate whatever amount of bytes for arr in one call to malloc() and arr points to whatever memory you allocated:

int nreg = 10;
struct student *pt = malloc(sizeof(struct student) + (nreg - 1) * sizeof(int));
pt->reg = nreg;
pt->arr[3] = 12; // pt->arr is the array you allocated with nreg*sizeof(int)

2 Comments

One should use Flexible Array Member from C99 int arr[]. Zero sized array is UB in standard C
@tstanisl right, I remember I used to use 1-sized array for that purpose. I edited.
0

In the structure, we may only have a pointer to the array we want to store in memory or a pointer to an array of a fixed size (For example: int arr[10];) The array will have to be dynamically allocated later if we choose the first option. We cannot give structure members default values or handle allocation of memory in the structure itself. I believe this is what you're looking for:

#include<stdio.h>
#include<stdlib.h>

struct student {
    int *arr;
    int reg;
};


void main()
{
    struct student *ptr = (struct student*) malloc(sizeof(struct student));
    ptr->reg = 10;
    ptr->arr = (int*) malloc(10 * sizeof(int));
    ptr->arr[0] = 100;
    printf("register no : %d\n",ptr->reg);
    printf("register no : %d\n",ptr->arr[0]);
    return;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.