The function is not updating the caller’s value of client_details, you need one more level of pointer:
void func(struct client_detail **client_details,int *size){
*client_details = realloc(*client_details, (*size + 1) * sizeof(**client_details));
*size = *size + 1;
}
int main()
{
struct group_detail *group_details = malloc(0 * sizeof(*group_details));
struct client_detail *client_details = malloc(1 * sizeof(*client_details));
int size = 0;
int j = 0;
while(j < 100){
func(&client_details,&size);
j++;
}
return 0;
}
Similar as with size, if you just wanted the value, the type would be int
. Changing an int size
variable inside the function would not have any effect on the caller. You want to change it, so you make a pointer to int int *
and dereference it to read/write the value.
With void func(struct client_detail *client_details,int *size){
you are getting the client_details pointer by value and changing that will have no effect on the caller’s variable. You need to add a pointer-to-it similar as was done for size.
0
solved malloc: *** error for object 0x147606ac0: pointer being realloc’d was not allocated