长路漫漫,唯剑作伴--基础

本文详细介绍了C语言中结构体的定义与使用,包括结构体变量、数组及与指针结合的应用,并探讨了枚举类型的创建与使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、结构体

结构体定义

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

结构体变量

struct stu stu1, stu2;
    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在学习小组
        float score;  //成绩
    } stu1, stu2;
    struct{  //没有写 stu
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在学习小组
        float score;  //成绩
    } stu1, stu2;

结构体访问

    #include <stdio.h>
    int main(){
        struct{
            char *name;  //姓名
            int num;  //学号
            int age;  //年龄
            char group;  //所在小组
            float score;  //成绩
        } stu1;
        //给结构体成员赋值
        stu1.name = "Tom";
        stu1.num = 12;
        stu1.age = 18;
        stu1.group = 'A';
        stu1.score = 136.5;
        //读取结构体成员的值
        printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", stu1.name, stu1.num, stu1.age, stu1.group, stu1.score);
        return 0;
    }

结构体赋值

    struct{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组
        float score;  //成绩
    } stu1, stu2 = { "Tom", 12, 18, 'A', 136.5 };

二、结构体数组

    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组 
        float score;  //成绩
    }class[5];
    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组 
        float score;  //成绩
    }class[5] = {
        {"Li ping", 5, 18, 'C', 145.0},
        {"Zhang ping", 4, 19, 'A', 130.5},
        {"He fang", 1, 18, 'A', 148.5},
        {"Cheng ling", 2, 17, 'F', 139.0},
        {"Wang ming", 3, 17, 'B', 144.5}
    };
    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组 
        float score;  //成绩
    }class[] = {
        {"Li ping", 5, 18, 'C', 145.0},
        {"Zhang ping", 4, 19, 'A', 130.5},
        {"He fang", 1, 18, 'A', 148.5},
        {"Cheng ling", 2, 17, 'F', 139.0},
        {"Wang ming", 3, 17, 'B', 144.5}
    };

三、结构体和指针

    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组
        float score;  //成绩
    } stu1 = { "Tom", 12, 18, 'A', 136.5 };
    //结构体指针
    struct stu *pstu = &stu1;
    struct stu{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组
        float score;  //成绩
    } stu1 = { "Tom", 12, 18, 'A', 136.5 }, *pstu = &stu1;
    #include <stdio.h>
    int main(){
        struct{
            char *name;  //姓名
            int num;  //学号
            int age;  //年龄
            char group;  //所在小组
            float score;  //成绩
        } stu1 = { "Tom", 12, 18, 'A', 136.5 }, *pstu = &stu1;
        //读取结构体成员的值
        printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", (*pstu).name, (*pstu).num, (*pstu).age, (*pstu).group, (*pstu).score);
        printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", pstu->name, pstu->num, pstu->age, pstu->group, pstu->score);
        return 0;
    }

四、枚举

    enum week{ Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun };
    enum week a = Mon, b = Wed, c = Sat;

 五、C++结构体

     //定义一个结构体,类型为struct Student
2      struct  Student     
3      {
4          string name;
5          double eng;
6          double ch;
7      }; 
8       
9      //定义了一个结构体,类型为struct Student;且定义了一个结构体实例,名叫Stu
10      struct  Student    
11      {
12          string name;
13          double eng;
14          double ch;
15      }Stu; 
16       
17      //定义了无名的结构体,且定义了一个结构体实例,名叫Stu
18      struct 
19      {
20          string name;
21          double eng;
22          double ch;
23      }Stu; 
24       
25      //重定义结构体,类型为struct Student 或者是Stu
26      typedef struct  Student  
27      {
28          string name;
29          double eng;
30          double ch;
31      }Stu; 
32       
33      //重定义结构体,类型为Stu
34      typedef struct  
35      {
36          string name;
37          double eng;
38          double ch;
39      }Stu;

 六、运算符

 七、OC枚举

//NS_ENUM,定义状态等普通枚举
typedef NS_ENUM(NSUInteger, TTGState) {
    TTGStateOK = 0,
    TTGStateError,
    TTGStateUnknow
};

//NS_OPTIONS,定义选项
typedef NS_OPTIONS(NSUInteger, TTGDirection) {
    TTGDirectionNone = 0,
    TTGDirectionTop = 1 << 0,
    TTGDirectionLeft = 1 << 1,
    TTGDirectionRight = 1 << 2,
    TTGDirectionBottom = 1 << 3
};

 

转载于:https://www.cnblogs.com/zhuyiios/p/6715205.html

错误: 找不到符号 Intent intent = new Intent(this,notification.class); ^ 符号: 类 notification 位置: 类 MainActivitypackage com.example.myapplication; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Build; import android.view.View; public class MainActivity extends AppCompatActivity { private static final String TAG = "notification"; // 通知管理器对象 private NotificationManager manager; // 通知对象 private Notification notification; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notifacation); // 1、创建【NotificationManager】通知管理器对象 manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); // 2、设置通知渠道 // 判断是否Android 8.0版本以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建【NotificationChannel】通知渠道 NotificationChannel channel = new NotificationChannel("test", "通知的内容: 您有一条新的消息!", NotificationManager.IMPORTANCE_HIGH); // 通知管理器添加通知渠道 manager.createNotificationChannel(channel); } // 设置跳转页面 Intent intent = new Intent(this,notification.class); // 创建PendingIntent PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); // 3、创建【Notification】通知对象 notification = new NotificationCompat.Builder(this, "test") .setContentTitle("测试通知标题") .setContentText("通知的具体内容: 路漫漫唯剑作伴") // 通知显示的小图标 .setSmallIcon(R.drawab
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值