Passing function structure to C

If I have the following: -

struct foo
{
  int a;
  int *b;
} bar;

void baz(struct foo qux)
{

}

Did I understand correctly that passing barin baz()causes the local copy to barbe pushed onto the stack? If so, which copy? in C ++, I assume that it will call the copy constructor or copy constructor by default, but I really don't know how this will work in C.

Does C have the concept of a default copy constructor and does it have a name? Is there anything you can do to make a deep copy? (Hypothetically). The only way I could think of is to make a deep copy and then pass it to the function.

, foo, , . , , , . , ; ?

+5
2

, baz() , ?

.

, C.

, ++; . , - " " memcpy.

, , .

struct , ; struct , , , , ( , C ++ ).

, ; ?

, ( ) - , ( " " , /).

"pass by reference", , ( ). , ( ++), const, , .

+4

. .

    #include <stdio.h>
    struct foo
    {
        int a;
        int *b;
    } bar;
    void baz(struct foo qux)
    {
        bar.a = 2; // if its a different copy then printf on main should print 1 not 2.
        *bar.b = 5; // if its a different copy then printf on main should print 5 not 4. since the place pointer pointing to is same
    }
    int main(){
        bar.a=1;
        bar.b = (int*)malloc(sizeof(int));
        *bar.b = 4;
        baz(bar); // pass it to baz(). now the copy of bar in the stack which is what baz going to use
        printf("a:%d | b:%d\n",bar.a,*bar.b);
        //answer is  2 and 5
        /*So indeed it does a shallow copy thats why we lost the "4" stored in bar.b it did not created new space in heap to store "5" instead it used the same space that b was pointing to.
        */
    return 0;
    }
+1

All Articles