• 结构体的基本操作


     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 
     6 //定义一个结构体
     7 //定义一个数据类型。固定内存大小的别名,还没有分配内存
     8 /*struct Teacher
     9 {
    10     char name[5];
    11     int age;
    12 };*/
    13 typedef struct Teacher
    14 {
    15     char name[64];
    16     int age;
    17     int id;
    18 }Teacher;
    19 
    20 
    21 struct Student
    22 {
    23     char name[64];
    24     int age;
    25 }s1,s2;//定义类型 同时定义变量
    26 
    27 struct
    28 {
    29     char name[64];
    30     int age;
    31 
    32 }s3, s4;    //匿名类型 定义变量
    33 
    34 //初始化变量的三种方法
    35 //定义变量 然后初始化
    36 //
    37 Teacher t7 = { "aaaaa", 18, 01 };    //全局
    38 struct Student2
    39 {
    40     char name[64];
    41     int age;
    42 }s5 = { "names", 21 };
    43 
    44 struct
    45 {
    46     char name[64];
    47     int age;
    48 
    49 }s6 = { "names", 30 };
    50 int main()
    51 {
    52     //struct  Teacher t1;    //告诉C编译器给我分配内存
    53     Teacher t1;
    54     Teacher t2 = { "aaaaa", 18, 01 };    //定义变量 然后初始化
    55 
    56     t1.age = 32;    //t1. 的.是寻址操作 计算t1相对于t1大变量的     
    57     //偏移量 ===》计算在cpu中进行,没有操作内存
    58 
    59     //通过指针的方式 操作 内存空间
    60     {
    61         Teacher *p = NULL;
    62         p = &t2;
    63         printf("p->age:%d
    ", p->age);    //-> 是寻址操作 相对于t2大变量的
    64                                         //偏移量 ===》计算在cpu中进行,没有操作内存
    65         printf("p->name:%s
    ", p -> name);
    66     }
    67     strcpy(t1.name, "张三");
    68     printf("ti.name%s
    ", t1.name);
    69     system("pause");
    70     return 0;
    71 }
  • 相关阅读:
    拳击游戏(虚函数应用)
    虚函数的使用
    继承中的二义性归属问题
    继承的作用以及在子类中初始化所有数据的方法
    Exploring ES2016 Decorators
    Storage information for PWA application
    浏览器中常见网络协议介绍
    vuex所有核心概念完整解析State Getters Mutations Actions
    搭建一个webpack微服务器
    nodeJS接入微信公众平台开发
  • 原文地址:https://www.cnblogs.com/linst/p/4867596.html
Copyright © 2020-2023  润新知