0
struct info
{
    int val;
};

void copy(struct info ** dst, struct info * src)
{
    *dst = (struct info *)malloc(sizeof(struct info));
    **dst = *src;
}

int main()
{
    struct info *a, *b; 

    a = (struct info *)malloc(sizeof(struct info));
    a -> val = 7;
    copy( , );

    a -> val = 9;
    printf("%d", b->val);
}

I have tried (b, a), (*b, *a), (b, *a) and so one but the argument is always unexpected by the compiler. Have been trying for an hour with no result - just a half melted brain.

Ahmad Khan
  • 2,655
  • 19
  • 25
banana1337
  • 25
  • 1
  • 4

1 Answers1

2

The first argument is supposed to be a pointer to a pointer. Since b is a pointer, you need to take its address, which is &b.

The second argument is supposed to be a pointer. a is a pointer, so you just pass it directly.

copy(&b, a);

* is for indirecting through a pointer to access what it points to. That's the exact opposite of what you want, which is to get a pointer to the variable itself. You use * inside the copy function to access what the pointers given point to.

BTW, you should also see Do I cast the result of malloc?

And don't forget to free the structures when you're done using them.

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612