C结构体 直接赋值及 memcpy

一、C结构体可直接 赋值

二、C结构体可memcpy()


#include 
#include 


typedef struct mystruct
{
    int a;
    short b;
    char sh[16];
}mystruct_T;




int main(int argc, const char *argv[])
{   
    mystruct_T st1, st2;
    mystruct_T *pst;


    char cc[128];


    memset(cc, 0, sizeof(cc));


    st1.a = 1;
    st1.b = 1;
    strcpy(st1.sh, "hello body");
    
    memset(&st2, 0, sizeof(mystruct_T));


    printf("st1.a = %d\n", st1.a);
    printf("st1.b = %d\n", st1.b);
    printf("st1.sh: %s\n", st1.sh);
    
    printf("---------------------\n");
    printf("st2.a = %d\n", st2.a);
    printf("st2.b = %d\n", st2.b);
    printf("st2.sh: %s\n", st2.sh);


    st2 = st1;
    printf("====================\n");
    printf("st2.a = %d\n", st2.a);
    printf("st2.b = %d\n", st2.b);
    printf("st2.sh: %s\n", st2.sh);


    pst = (mystruct_T *)cc;
    memcpy(pst, &st1, sizeof(mystruct_T));


    printf("..................\n");
    printf("pst->a = %d\n", pst->a);
    printf("pst->b = %d\n", pst->b);
    printf("pst->sh: %s\n", pst->sh);


    return 0;
}
C结构体 直接赋值及 memcpy_第1张图片

你可能感兴趣的:(C/C++)