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結構體類型定義的時候不能復初值

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM