C 語言之結構體

來源:互聯網
上載者:User

標籤:nbsp   turn   main   blog   char   har   printf   pre   stdio.h   

  1 #include <stdio.h>  2   3 #include <struct.h>  4   5 #include <string.h>  6   7    8   9 // struct 是結構體的關鍵詞。 10  11 typedef struct Student 12  13 { 14  15     char name[30]; 16  17     char addr[50]; 18  19     int age; 20  21 }STU;//   STU=struct Student 22  23   24  25   26  27   28  29 int main(int argc, const char * argv[]) { 30  31     STU student1; 32  33     STU student2; 34  35      36  37     strcpy(student1.name,"張三"); 38  39     strcpy(student1.addr,"北京"); 40  41     student1.age=20; 42  43      44  45     strcpy(student2.name,"李四"); 46  47     strcpy(student2.addr, "四川"); 48  49     student2.age=30; 50  51      52  53     printf("-------------利用指標訪問結構體成員。\n"); 54  55     STU *pointer1=&student1; 56  57     printf("student1.name = %s\n",pointer1->name); 58  59     printf("student1.addr = %s\n",pointer1->addr); 60  61     printf("student1.age = %d\n",pointer1->age); 62  63     STU *pointer2=&student2; 64  65     printf("student2.name = %s\n",pointer2->name); 66  67     printf("student2.addr = %s\n",pointer2->addr); 68  69     printf("student2.age = %d\n",pointer2->age); 70  71      72  73     printf("-------------利用指標訪問結構體地址和結構體中成員的地址。\n"); 74  75     printf("&student1 = %p\n",&student1); 76  77     printf("&(pointer->name) = %p\n",&(pointer1->name)); 78  79     printf("&(pointer->name) = %p\n",&(pointer1->addr)); 80  81     printf("&(pointer->name) = %p\n",&(pointer1->age)); 82  83   84  85      86  87     printf("-------------利用結構體訪問地址。\n"); 88  89     printf("&(student1.name) = %p\n",&(student1.name)); 90  91     printf("&(student1.addr) = %p\n",&(student1.addr)); 92  93     printf("&(student1.age) = %p\n",&(student1.age)); 94  95      96  97      98  99     100 101     return 0;102 103 }

 

C 語言之結構體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.