結構體2之變數賦值於字串

來源:互聯網
上載者:User

標籤:指標   log   變數   hone   getch   rcp   struct   字串   void   

 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         "[email protected]",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結構體類型定義的時候不能複初值

 

結構體2之變數賦值於字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.