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 ;
}
malloc()and anyvoid*. It is pointless, error prone and adds only clutter.