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 ;
}
CodePudding user response:
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;
}
CodePudding user response:
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);
}
CodePudding user response:
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)
CodePudding user response:
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;
}