c语言结构体2之变量赋值于字符串

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 struct dangdang
 5 {
 6     char email[30];
 7     char name[30];
 8     char addr[100];
 9     int num;
10     int bugnum;
11     char tel[20];
12     char phone[20];
13     double RMB;
14     int dangdang;//成员名可以和类名同名
15 }dx,dy= {
16         "1111dfd1f@dfd",
17         "lala",
18         "chongq",
19         54,
20         543532656,
21         54.32,
22     };
23 
24 void main()
25 {
26     dx=dy;
27     printf("%s",dx.name);
28     //结构体变量可以直接赋值 
29     //但是必须是同一个类型
30 
31 }
 1 struct ours
 2 {
 3     int num;//结构体类型定义的时候不能复初值
 4     char str[100];
 5 };
 6 
 7 void main()
 8 {
 9     struct ours ol = {10,"hello"};
10     struct ours o2 = ol;//结构体直接赋值的时候,整体即使是字符串也可以
11     printf("%d,%s",o2.num,o2.str);
12 
13     //o2.str = o2.str;这个时候是指针常量 字符串不能直接赋值
14 
15     //字符串的赋值方式
16     sprintf(ol.str,o2.str);
17     strcpy(ol.str,o2.str);
18 
19     getchar();
20 }

注意:

1结构体直接赋值的时候,整体即使是字符串也可以

2o2.str = o2.str;这个时候是指针常量 字符串不能直接赋值

3字符串的赋值方式
 sprintf(ol.str,o2.str);
 strcpy(ol.str,o2.str);

4结构体类型定义的时候不能复初值

原文地址:https://www.cnblogs.com/lanjianhappy/p/6011465.html