C语言结构体对象间直接赋值

来源:互联网 发布:小米3可以用4g网络吗 编辑:程序博客网 时间:2024/06/06 03:54

C语言中变量间互相赋值很常见,例如:

int a,b;a = b;

结构体也是变量(自定义变量),两个结构体之间直接赋值按道理应该也是可以的吧,说实话之前还从没遇到过将一个结构体对象赋值给另一个结构体对象的(见识太浅),那么下面做一个测试看看:

#include "stdio.h"struct test{    int a;    int b;    int c;    char *d;};int main(){    struct test t1 = {1,2,3,"tangquan"};    struct test t2 = {0,0,0,""};    printf("%d,%d,%d,%s\r\n",t2.a,t2.b,t2.c,t2.d);    t2 = t1;    printf("%d,%d,%d,%s\r\n",t2.a,t2.b,t2.c,t2.d);    return 0;}

运行结果是:

tq@ubuntu:/mnt/hgfs/vmshare$ gcc test.c -o tartq@ubuntu:/mnt/hgfs/vmshare$ ./tar 0,0,0,1,2,3,tangquan

很显然赋值之后t2结构体的内容全部赋值为了t1的内容,假设正确。那么C++中的类之间的相互赋值应该也是可以的了?

原创粉丝点击