C語言結構體1.1

來源:互聯網
上載者:User

標籤:整型   main   size   字元型   定義變數   col   struct   margin   stdio.h   

 結構體組成

        struct  結構體名;

        類型名   成員名;

 建立結構體

         結構體名  類型名

             {

                成員;

              };

     建立一個關於學生資訊的結構體(名字,年齡,性別,學號,成績);

結構體定義

//結構體聲明

        struct  student

{

  char  name[20];    //名字為字元型

  int  age;       //年齡為整型

  char  sex;      //性別為字元型

  char  id[20];     //學號為字元型

  float  score;     //成績為實型(小數形式)

}

  struct是聲明結構體類型時所必須使用的關鍵字,不能省略;

定義變數

struct  student  (student1,student2)   //先聲明結構體類型,再定義該類型的變數

{

  char  name[20];    //名字為字元型

  int  age;       //年齡為整型

  char  sex;      //性別為字元型

  char  id[20];     //學號為字元型

  float  score;     //成績為實型(小數形式)

}student1,student2;     //變數名  在宣告類型的同時定義變數(全域變數)

結構體變數初始化

struct  student

{

  char  name[20];    //名字為字元型

  int  age;       //年齡為整型

}a={“Li Hua”,16};

例:

#include<stdio.h>
main()
 {
  struct student
    {
      char name[20];
      int age;
    }a={"Li Hua",16};  //變數初始化
  printf("Name:%s\nAge:%d",a.name,a.age);  //"."是成員運算子 可以吧a.name作為一個整體
}

用類型的結構體變數可以互相賦值;            可以引用結構體變數成員的地址,也可以引用結構體變數地址:              如:scanf("%d",&a.age);printf("%d",&a); 待.....

              

 

 

 

C語言結構體1.1

聯繫我們

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