ios開發中的C語言學習—— 結構體簡介,ios語言學習

來源:互聯網
上載者:User

ios開發中的C語言學習—— 結構體簡介,ios語言學習

  在開發過程中,經常會需要處理一組不同類型的資料,比如學生的個人資訊,由姓名、年齡、性別、身高等組成,因為這些資料是由不同資料類型組成的,因此不能用數組表示,對於不同資料類型的一組資料,可以採用結構體來進行儲存。當然,對於物件導向的語言來說,最好是用類來表示,但是C語言是面向過程的,因此選擇用結構體來表示。

一.結構體的定義
struct 結構體名{        類型名 成員名1;        類型名 成員名2;        ... ...        類型名 成員名n;};

 

二.結構體的變數聲明
1. 先定義結構體類型,再定義變數

代碼

////  main.c//  結構體////  Created by jerei on 15-12-27.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#include <stdio.h>/** *  定義學生資訊的結構體 */struct student{    char name[100]; //姓名    unsigned int age; //年齡    char sex; //性別    double height; //身高};int main(int argc, const char * argv[]) {        //聲明結構變數struct student student1;struct student student2;        return 0;}

 

三.定義結構體類型的同時定義變數

代碼

////  main.c//  結構體////  Created by jerei on 15-12-27.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#include <stdio.h>/** *  定義學生資訊的結構體,並聲明兩個學生結構變數student1和student12 */struct student{    char name[100]; //姓名    unsigned int age; //年齡    char sex; //性別    double height; //身高} student1, student2;int main(int argc, const char * argv[]) {        return 0;}

 

四. 直接定義結構體類型變數,省略類型名

代碼

////  main.c//  結構體////  Created by jerei on 15-12-27.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#include <stdio.h>/** *  直接聲明兩個結構體變數student1和student2 */struct{    char name[100]; //姓名    unsigned int age; //年齡    char sex; //性別    double height; //身高} student1, student2;int main(int argc, const char * argv[]) {        return 0;}
 

 

五.結構體的嵌套
1 結構體中可以包含,但是不允許對結構體本身遞迴使用。

代碼

////  main.c//  結構體////  Created by jerei on 15-12-27.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#include <stdio.h>/** *  定義日期結構體 */struct date{    unsigned int year;    unsigned int month;    unsigned int day;};/** *  定義學生結構體 */struct student{    char name[100]; //姓名    unsigned int age; //年齡    char sex; //性別    double height; //身高    struct date birthday; //出生日期 (date結構體)};int main(int argc, const char * argv[]) {    return 0;

 

六.結構體的初始化

<一> 結構體變數可以在聲明的時候一次性給多個成員初始化,但是需要注意的是初始化的順序必須和定義結構體成員的順序一樣,初始化成員的個數是可以少於總成員個數。

<二> 聲明結構變數後,可以採用結構變數名.成員名來為其賦值或取值。

<三> 聲明結構變數後,可以整體接收相同類型的其他結構變數的值。

代碼

///  main.c//  結構體////  Created by jerei on 15-12-27.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#include <stdio.h>/** *  定義日期結構體 */struct date{    unsigned int year;    unsigned int month;    unsigned int day;};/** *  定義學生結構體 */struct student{    char name[100]; //姓名    unsigned int age; //年齡    char sex; //性別    double height; //身高    struct date birthday; //出生日期};int main(int argc, const char * argv[]) {        //<一> 一次性給多個成員賦值    struct date birth1 = {1992, 1, 1};    struct student student1 ={"jredu", 21, 'f', 180.0, birth1};     //<二>對單個成員賦值    student1.age = 20;student1.height = 178.0;    //<三>相同類型的變數間可進行整體賦值struct student student2 = student1;        return 0;}

 

七.結構體的使用

  結構體是我們自訂的一種資料類型,但是實際上和系統提供給我們的基礎資料型別 (Elementary Data Type)的使用是一樣的,因此,除了可以用結構做變數,還可以用結構體做數組、指標、函數。

1 結構數組

  用數組來儲存一組結構體類型的變數,比如存放一組學生的結構數組。

  在使用結構數組的時候和上面講的結構變數一樣,同樣可以通過三種方式來得到結構數組。

代碼

/** *  <一>先定義結構體 */struct student{    char name[100]; //姓名    unsigned int age; //年齡    char sex; //性別    double height; //身高} ;int main(int argc, const char * argv[]) {        //再聲明結構數組    struct student stus[10];        return 0;}

 

代碼

/** *  <二>定義結構體同時直接聲明結構數組 */struct student{    char name[100]; //姓名    unsigned int age; //年齡    char sex; //性別    double height; //身高} stus[10];

 

代碼

/** *  <三>直接聲明結構數組 */struct {    char name[100]; //姓名    unsigned int age; //年齡    char sex; //性別    double height; //身高} stus[10];

 

2 指向結構體的指標

要想使用指標來間接改變資料,必須用相同類型的指標去指向對象。結構體類型的變數或者數組在使用的時候就需要使用結構體類型的指標。

代碼

////  main.c//  結構體////  Created by jerei on 15-12-27.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#include <stdio.h>/** *  定義結構體 */struct student{    char *name; //姓名    unsigned int age; //年齡} ;int main(int argc, const char * argv[]) {        //聲明結構變數    struct student student1 = {"jredu", 21};        //定義一個結構指標    struct student *ptr = &student1;        //訪問結構成員,比如得到學生資訊    //方式1:直接使用結構變數    printf("name = %s,age = %u\n",student1.name, student1.age);    //方式2:通過指標得到結構變數    printf("name = %s,age = %u\n", (*ptr).name, (*ptr).age);    //方式3:直接用指標    printf("name = %s,age = %u\n",ptr->name, ptr->age);        return 0;}

 

3 結構體做函數的參數

代碼

////  main.c//  結構體////  Created by jerei on 15-12-27.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#include <stdio.h>/** *  定義結構體 */struct student{    char *name; //姓名    unsigned int age; //年齡} ;void func1(struct student tempStu);void func2(struct student *ptrStu);int main(int argc, const char * argv[]) {        //聲明結構變數    struct student student1 = {"jredu", 21};    struct student student2 = student1;        //調用參數為結構變數的函數    func1(student1);    printf("student1 name = %s\n",student1.name);        //調用參數為結構變數的函數    func2(&student2);    printf("student2 name = %s\n",student2.name);        return 0;}void func1(struct student tempStu){    tempStu.name = "apple";}void func2(struct student *ptrStu){    ptrStu->name = "apple";}

 

八、結構體的簡化

 typedef可以對資料類型進行重新命名,因此在定義結構體的時候可以使用它來簡化操作。

代碼

////  main.c//  結構體////  Created by jerei on 15-12-27.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#include <stdio.h>/** *  定義結構體 */typedef struct {    char *name; //姓名    unsigned int age; //年齡} Student;int main(int argc, const char * argv[]) {        //聲明結構變數    Student student1 = {"jredu", 21};        return 0;}

 

作者:傑瑞教育
出處:http://www.cnblogs.com/jerehedu/ 
著作權聲明:本文著作權歸煙台傑瑞教育科技有限公司和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。
技術諮詢: 

相關文章

聯繫我們

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