Struct mystruct a = b; Is it really? Direct assignment of structural variables, is it really in C?

I have the following code.

struct rectangle {
int length;
int breadth;
};

int main (void) 
{
    struct rectangle a,b;
    a.length = 10;
    a.breadth = 5;

    b=a;  
    printf("\nValues of b.length = %d, b.breadth=%d\n",b.length,b.breadth);
    return 0;
}

Is the above assignment a valid expression? (b = a) I did it this way inside my project. I received a feedback message , this type of assignment is incorrect, and I had to use memcpy . I printed the values ​​of b and checked. Values ​​are correct. I was wondering why the above task is wrong? If this is not correct, what happens when you pass a structural variable to a function and intercept it in a separate variable? I hope that I clearly understand my question. Please come back to me if I am unclear on my question.

+5
3

, , . , , , . , , , memcpy(), (!) .

, memcpy() , , . , , , , .

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

+8

, , , , .

, , - memcpy, , - , , .

,

struct a some_function(struct a)
{
    a.width = 99;
    return a;
}

, , , , , .

, C ...

, , , .

memcpy(&b, &a, sizeof(b));
b = a;

, ; - hokum, C (.. C89), , .

, -, pre-ansi ( 1986 ) unix.

/tmp> gcc a.c
a.c:
C-68000U 1.8.0 Copyright (c)1985,1986 Green Hills Software, Inc.
linking a:
/tmp> ./a
10, 5

, , ! .

, , , . 68k - //, , .

, , , - ,

    ;       line  #13 memcpy(&b, &a, sizeof(b));
     140     PEA    0x8           // size
     144     PEA    (-8,FP)       // &a
     148     PEA    (-16,FP)      // &b
     152     JSR    memcpy        // call memcpy

    ;       line  #14
     170     LEA    (-8,FP),A1   // &a
     174     LEA    (-16,FP),A0  // &b
     178     MOVE.L (A1)+,(A0)+  // copy first element of struct
     180     MOVE.L (A1)+,(A0)+  // copy second element

, b=a,

     170     LEA    (-112,FP),A1
     174     LEA    (-224,FP),A0
     178     MOVEQ  #27,D0
     180     MOVE.L (A1)+,(A0)+
     182     DBF    D0,main+60

, memcpy, .

, - - , ( )

+4

, , , .

, , , , struct.

, memcpy C, ++ ( POD ). , , .

0

All Articles