美文网首页
C语言和OC的结构体(struct)

C语言和OC的结构体(struct)

作者: _菩提本无树_ | 来源:发表于2020-03-18 10:09 被阅读0次

Struct(结构体)

1.结构体定义

结构体是一种自定义的数据类型,是创建变量的模板,不占用内存,结构体变量包含了实实在在的数据,才会占用内存
struct 结构体名称{

    结构体内部包含的变量或数组或其他结构体,结构体包含的内容类型可以是不同的

};//";"这个结尾符不能缺失
//结构体成员的定义方式与变量数组的定义方式相同,但是不能进行初始话

2.结构体变量

struct Student {
    
    char *name;  //姓名
    int num;  //学号
    int age;  //年龄
    char group;  //所在学习小组
    float score;  //成绩

};
//定义两个结构体变量,注意这种定义变量的方式struct关键字不能缺少
struct Student student1,student2;

使用typedef可以为结构体重命名而且更加容易操作
typedef struct Student{
    //...
}studentN;
接下来定义studentN的变量时可以直接使用studentN定义不用再写struct关键字

//还有一种定义结构体变量的方式,即在定义结构体的同时定义结构体变量
(C语言版,OC在.m文件中可以这么写,在.h文件中定义需要使用typedef修饰)
struct Student{
    //...
} student1,student2;
//如果不需要自己再次定义变量,那么结构体名可以省去
struct{
    //...
}student1,student2;

//给变量赋值
student1.name = "测试";
//也可以整体赋值,但是仅限于在定义结构体变量的时候,在使用过程中只能一一赋值
//OC中在.m文件里面可以这么写,.h文件中定义的不能
struct Student {
    
    char *name;  //姓名
    int num;  //学号
    int age;  //年龄
    char group;  //所在学习小组
    float score;  //成绩

} student1 = {"",1,32,'1',123.0};
//或者这个时候定义
Student student1 = {"",1,32,'1',123.0};

//取值
NSString *name = student1.name;

3.结构体数组

struct Student{
    //...
}class[5];
//表明班级有5个学生

//对结构体整体赋值的时候也可以不用指定数组长度
struct Student{
    //...
}class []= {},{},{},{};

//取值也很简单,class[0].name;

4.C语言结构体指针

struct Student{
    char *name;
    int score;
} stu = {"Tom",123};

//结构体指针
struct student * pstu = &stu;

//也可以在定义结构体的时候,同时定义结构体指针
struct Student{
    char *name;
    int score;
} stu = {"Tom",123},*pstu = &stu;

//注意结构体和结构体变量是两个概念,我们可以去结构体变量的地址,但是不能取结构体的地址

//结构体指针获取结构体成员
(1).第一种方式通过点语法
(*pstu).name;
(2).第二种常用的是通过箭头语法
pstu->name;

//结构体变量作为参数时,尤其是当结构体变量是数组数据很大时,不要使用结构体变量传值,
最好的办法是使用结构体指针传值.
-(void)test:(Student *)stu{
  stu->name;
}

5.C语言共用体

//格式和结构体很像,但是存储方式不同,结构体每个数据占用一段内存,共用体是所有数据公用一段内存,
//所以修改一个数据,所有的数据都会受影响.
union 共用体名{
    成员列表
};

6.使用struct结构体实现链表

struct Student{

    NSString *name;
    float score;

}

- (void)makeLinkList{
    //简单的打印链表信息
    struct Student a,b,c, *head,*p;
    a.name = @"我"; a.score = 123;
    b.name = @"你"; b.score = 231;
    c.name = @"他"; c.score = 983;
    head = &a;
    a.next = &b;
    b.next = &c;
    c.next = NULL;
    p = head;
    
    do {
        NSLog(@"%@,%f",p->name,p->score);
        p = p->next;
//        p = (*p).next;
    } while (p != NULL);
    
}

7.相关代码

DDRootViewController.h
//
//  DDStructViewController.h
//  AdvancePlan
//
//  Created by 董德帅 on 2020/3/17.
//  Copyright © 2020 www.dong.com 董德帅. All rights reserved.
//

#import "DDRootViewController.h"


NS_ASSUME_NONNULL_BEGIN
//这种方式定义struct不能直接声明struct变量
struct student{
    int n;
};

//使用typedef修饰的struct可以直接定义struct变量
typedef struct teacher {
    
    NSString * name;
    int age;

} teacher1,teacher2;
//如果值需要一个变量可以省去结构体名称
typedef struct{
    
    NSString *name;
    
} teacher;
@interface DDStructViewController : DDRootViewController

@end

NS_ASSUME_NONNULL_END



DDRootViewController.m
//
//  DDStructViewController.m
//  AdvancePlan
//
//  Created by 董德帅 on 2020/3/17.
//  Copyright © 2020 www.dong.com 董德帅. All rights reserved.
//

#import "DDStructViewController.h"
struct Student{
    NSString *name;
    float score;
    struct Student *next;
}s = {};
@interface DDStructViewController ()

@end

@implementation DDStructViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    teacher tes = {@"",12};
    
    struct Studet{
        int m;
        int n;
    } stru,srr = {1,2};

    
    //通过struct制作一个简单的链表
    [self makeLinkList];
    
    // Do any additional setup after loading the view.
}

- (void)makeLinkList{
    struct Student a,b,c, *head,*p;
    a.name = @"我"; a.score = 123;
    b.name = @"你"; b.score = 231;
    c.name = @"他"; c.score = 983;
    head = &a;
    a.next = &b;
    b.next = &c;
    c.next = NULL;
    p = head;
    
    do {
        NSLog(@"%@,%f",p->name,p->score);
        p = p->next;
//        p = (*p).next;
    } while (p != NULL);
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

相关文章

  • 【OC梳理】结构体、枚举

    结构体(struct) OC中的结构体(struct),其实就是C语言中的结构体(struct)常见使用方法。OC...

  • C语言和OC的结构体(struct)

    Struct(结构体) 1.结构体定义 2.结构体变量 3.结构体数组 4.C语言结构体指针 5.C语言共用体 6...

  • 嵌入式学习笔记19.11.25

    c语言结构体 结构体一般定义全局变量 struct stu{//struct 定义结构体 stu 结构体名称 in...

  • 结构体

    OC中 定义结构体,MyTest 可省略,结构体类型名为 Test typedef struct MyTest {...

  • C++总结

    结构体成员变量是什么? C++结构体怎么定义 struct aa { }或者typedef struct aa {...

  • C语言基础6

    结构体概述 1 在C语言中,结构体(struct)指的是一种数据结构,是C语 言中构造类型的其中之一。 2 在实际...

  • 01-OC对象的本质

    OC是通过C/C++的什么数据结构实现我们的OC对象呢 结构体--OC对象的本质就是C/C++的结构体 Class...

  • iOS Class结构分析

    objc_class结构体 类在OC中是objc_class的结构体指针 typedef struct objc_...

  • C++ 数据抽象

    struct C++的第一步,函数可以放在struct内部。 C的struct需要typedef,C++的结构体不...

  • 二次开始 - struct 与 enum

    struct 结构体,oc中灰常常见,用于存储多种数据类型。 第一种定义,定义结构体,不声明变量 struct P...

网友评论

      本文标题:C语言和OC的结构体(struct)

      本文链接:https://www.haomeiwen.com/subject/aixqyhtx.html