Hello everyone,
I've been learning C for a few months now and I'm stuck on an aspect of passing pointers.
Here's what I think is a pointer to an array pointer of struct pointers:
Code:
#include <stdio.h>
#include <stdlib.h>
#define ARR_SIZE 5
struct my_struct{
int num_one;
int num_two;
};
int main (void)
{
struct my_struct* struct_arr[ARR_SIZE];
for(int i = 0; i < ARR_SIZE; i++)
{
struct_arr[i] = malloc(sizeof(struct my_struct));
struct_arr[i]->num_one = i;
struct_arr[i]->num_two = i + 50;
}
struct my_struct *(*p)[] = &struct_arr;
for(int i = 0; i < ARR_SIZE; i++)
{
printf("array member: %d\n", i);
printf("num_one: %d - num_two: %d\n\n", (*p)[i]->num_one, (*p)[i]->num_two);
}
return 0;
}
I can access and modify the elements by using (*p)[i], as shown above. But what is the syntax to pass *(*p)[] to a function to modify?
Code:
void mod_arr (struct my_struct *(*ms))
{
for(int i = 0; i < ARR_SIZE; i++)
{
(*ms)[i]->num_one = 50;
(*ms)[i]->num_two = 30;
}
}
//I'm trying to call the function like this:
mod_arr(*(*p));
I've tried what feels like every variation of syntax in the arguments and I cannot find the correct way to pass. Thanks in advance!