I want to pass a structure pointer to a function that will dynamically create a array of structures at the location pointed to by the structure pointer that was passed. I am able to create and fill the array of structure successfully but when trying to print the data in the calling function using the pointer that was passed gives me a garbage values. Please help me know why my structure pointer is pointing to garbage and how can I access my data correctly.
The following is just some example code to demonstrate how the structure is passed and dynamically filled using malloc & realloc. this is INCORRECT method:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student *record);
int main()
{
struct student *record = NULL;
record = (struct student *)malloc(sizeof(struct student));
func(record);
if(record != NULL)
{
for(int i=0; i<2; i++)
{
printf(" 1 Id is: %d \n", record[i].id);
printf(" 1 Name is: %s \n", record[i].name);
printf(" 1 Percentage is: %f \n", record[i].percentage);
printf("\n");
}
}
else
{
printf("record pointer is null");
}
return 0;
}
void func(struct student *record1)
{
for(int i=0; i<2; i++)
{
if(i)
{
record1 = (struct student *)realloc(record1,sizeof(struct student)*(i+1));
}
record1[i].id=1;
strcpy(record1[i].name, "Raju");
record1[i].percentage = 86.5;
}
}
The following is a similar example using double pointer which is the CORRECT way to do this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student **record);
int main()
{
struct student *record = NULL;
func(&record);
if(record != NULL)
{
for(int i=0; i<2; i++)
{
printf(" 1 Id is: %d \n", record[i].id);
printf(" 1 Name is: %s \n", record[i].name);
printf(" 1 Percentage is: %f \n", record[i].percentage);
printf("\n");
}
}
else
{
printf("record pointer is null");
}
free(record);
return 0;
}
void func(struct student **record1)
{
*record1 = (struct student *)malloc(sizeof(struct student));
for(int i=0; i<2; i++)
{
if(i)
{
*record1 = (struct student *)realloc(*record1,sizeof(struct student)*(i+1));
}
(*record1)[i].id=1;
strcpy((*record1)[i].name, "Raju");
(*record1)[i].percentage = 86.5;
}
}