Update:
In response to your comment:
int main ( void )
{
int i=1, j=2;//local vars
struct person me;//a local, stack allocated, struct
void add_person(struct person *p, int p1, int p2);//prototype
//pass ADDRESS OF struct to function
add_person(&me, i, j);
return EXIT_SUCCESS;
}
void add_person(struct person *p, int p1, int p2)
{
p->some_int = p2;//changes the struct we declared in main
(*p).friends = newf2;//alt syntax: dereference pointer
}
Here, you see you have 2 ways of using a struct, either leave it as is, and use the indirection operator ->, or dereference it (*), and use direct access (.). Not all that hard, really...
For a better understanding, or at least a primer in pointers, I'd suggest this answer of mine, where I make an analogy between pointers, and a calendar. I've used this explanation to help a few people and all of them (up till now, no exception) found it quite helpful. It's a really simple way of thinking about pointers that isn't full proof (once you get into more complex stuff like decayed arrays of pointers to structs it doesn't quite stack up), but when learning, it should help you come to terms with the basics.
Ok, you have a pointer to a pointer. Great, so if you dereference that pointer, which you do (*p) you end up with a regular pointer to a struct. Great, but why would you then need the [*i], still? it's a pointer, not an array, unless [*i] is 0, you'll be accessing memory out of bounds, either write:
p[*i]->friends = newf2;//for example
or, if you want to make your life harder, still:
*(p+(*i)).friends = newf2;
That's just awful IMO, though...
Your approach (dereferencing + getting an index/offset) could perhaps work if you were to write this:
(*p)[0].friends = newf2;
But again, that's just making life difficult for yourself.
As is your passing pointers to ints to the function. Sure, sometimes you have to pass a pointer, but really, sending an int by copy isn't a lot of overhead:
void add_friend(struct person **p, int p1, int p2)
{//works fine
p[i]->friends = newf2;
}
//call like so:
struct person **pers_array = calloc(10, sizeof(*pers_array));//alloc memory for pointers
for (int i=0;i<10;++i) pers_array[i] = malloc(sizeof *pers_array[i]);//alloc struct
add_friend(pers_array, 1, 2);
And as ever, don't cast void * in C + every malloc and or calloc has to have a free that goes with it
*p1isn't accessing outside of the array bounds?(*p)[*i]top[*i]should do the trick, and check for NULL pointers being passed. As your code now stands, you're accessing a pointer tostruct personas though it was an array ((*p)dereferences first pointer, so you have*p, then you get[*i]offset), that can't be right. and as ever: don't cast void pointers returned bymalloc/realloc