黑马程序员 《ios零基础教程》--全局和局部变量、结构体、枚举 2014-4-2总结

2023-10-26

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------



前几天出差有事儿没学习,今天主要学习了复杂数据类型中的一些其他类型


一、其他数据类型

1.全局变量和局部变量

#include <stdio.h>

/*
 根据变量的作用域,可以分为:
 1.局部变量:
 1>定义:在函数(代码块)内部定义的变量(包括函数的形参)
 2>作用域:从定义变量的那一行开始,一直到代码块结束
 3>生命周期:从定义变量的那一行开始分配存储空间,代码块结束后,就会被回收
 4>没有固定默认的初始值就是0
 
 2.全局变量
 1>定义:在函数外面定义的变量
 2>作用域:从定义变量的那一行开始,一直到文件结尾(能被后面的所有函数共享)
 3>生命周期:程序已启动就会分配存储空间,程序退出时才会被销毁
 4>默认的初始值就是0
 */

int age;

void test()
{
    int age;
    age = 10;
    printf("%d\n",age);
}

int main(int argc, const char * argv[])
{
    printf("%d\n",age);
//    int age = 20;
    printf("%d\n",age);
    
    test();
    
    printf("%d\n",age);
    return 0;
}

2.结构体

1>基本使用
#include <stdio.h>

/*
 数组:只能由多个相同类型的数据构成
 
 结构体:可以由多个不同类型的数据构成
 */

int main(int argc, const char * argv[])
{
//    int ages[3]= {10, 11 , 29};
    
    //1.定义结构体类型
    struct Person
    {
        //里面的3个变量,可以称为是结构体的成员或者属性
        int age;//年龄
        double height;//身高
        char *name;//姓名
    };
    
    //2.根据结构类型,定义结构体变量
    struct Person p = {20, 1.55, "jack"};
    p.age = 30;
    p.name = "rose";
   
    printf("age=%d, name=%s, heigth=%f\n",p.age,p.name,p.height);
    
    /*错误写法
     struct Person p2;
     p2 = {20 ,1.67,"jack"};
     
     相当于
     int ages[5];
     age = {1 ,2 , 3 ,5 ,2};
     是错误的
     */
    
    struct Person p2 = {.height = 1.78 ,.name = "joke",.age = 46};
    printf("age=%d, name=%s, heigth=%f",p2.age,p2.name,p2.height);
    return 0;
}
2>内存分析
#include <stdio.h>

void test();
void test1();

int main(int argc, const char * argv[])
{
    test1();
    test();
    
    return 0;
}

void test1()
{
    struct Student
    {
        int age;//4个字节
        
        char a;//1个字节
        
        char *name;//8个字节
    };
    
    struct Student stu;
    //stu.age = 20;
    //stu.name = "jack";
    //补齐算法(对齐算法) 本节中有详解,看
    //结构体所占用得存储空间 必须是 最大成员字节数的倍数
    
    int s = sizeof(stu);
    printf("%d\n",s);
}


void test()
{
    //1.定义结构体类型(并不会分配存储空间)
    struct Date
    {
        int year;
        int month;
        int day;
    };
    
    //2.定义结构体变量(真正分配存储空间)
    struct Date d1 = {2011, 4 ,10};
    
    struct Date d2 = {2013 , 8 , 9};
    
    //会讲d1所有成员的值对应的值赋值给d2的所有成员
    d2 = d1;
    
    d2.year = 2010;
    printf("%d - %d -%d\n",d1.year ,d1.month ,d1.day);
    printf("%d - %d -%d\n",d2.year ,d2.month ,d2.day);
    
    d2.month = 12;
    
    /*
    printf("%p - %p -%p\n",&d1.year ,&d1.month ,&d1.day);
    
    int s = sizeof(d1);
    printf("%d\n",s);
     */
}

3>变量的多种定义
#include <stdio.h>

/*
 1.定义结构体变量的3种方式
 1>先定义类型,再定义变量(分开定义)
 struct Student
 {
    int age;
 };
 struct Student stu;
 
 2>定义类型的同时定义变量
 struct Student
 {
    int age;
 }stu;
 struct Student stu2;
 
 3>定义类型的同时定义变量(省略了类型名称)
 strct
 {
    int age;
 }stu;
 */

int main(int argc, const char * argv[])
{
    //定义结构体变量的第3种方式,但是这种方式只能使用一次,下次使用还需要重新定义
    struct{
        int age;
        char *name;
    }stu;
    
    /*结构类型不能重复定义  这是错误写法
     struct Student
     {
        int age;
     };
     
     struct Student
     {
        double height;
     };
     
     struct Student stu;
     */
    
    /*错误写法:结构体类型重复定义
     struct Student
     {
        int age;
        double height;
        char *name;
     }stu;
     
     struct Student
     {
     int age;
     double height;
     char *name;
     }stu2;
     
     */
    
    /*
     这句代码做了两件事情
     1.定义结构体类型
     2.利用新定义好的类型来定义结构体变量
     */
    
    //定义变量的第2种方式:定义类型的同时定义变量
    /*
     struct Student
     {
         int age;
         double height;
         char *name;
     }stu;
     
     struct Student stu2;
     */
    
    /*
     //定义变量的第1种方式:
     //1.类型
     struct Student
     {
        int age;
        double height;
        char *name;
     };
     
     //2.变量
     struct Student stu = {20 , 1.78 ,"jack"};
     */
    
    return 0;
}
4>结构体数组
#include <stdio.h>

int main(int argc, const char * argv[])
{
    struct RankRecord
    {
        int no;
        char *name;
        int score;
    };
    
    /*
     struct RankRecord r1 = {1 , "jack" , 5000};
     struct RankRecord r2 = {2 , "jim" , 500};
     struct RankRecord r3 = {3 , "jake" ,300};
     */
    
    //int ages[3] = {10 ,19 ,29};
    
    //int ages[3];
    //对齐算法
    //能存放3个结构体变量,每个机构体变量占16个字节
    
    struct RankRecord records[3]=
    {
        {1,"jack",5000},
        {2,"jim",500},
        {3,"jake",300}
    };
    
    records[0].no = 4;
    //错误写法
    //records[0] = {4, "rose" ,9000};
    
    for (int i = 0; i < 3; i++)
    {
        printf("%d\t%s\t%d\n",records[i].no,records[i].name,records[i].score);
    }
    
    //这里为什么size=24,而不是size=16
    printf("size=%d\n",sizeof(records[1]));
    return 0;
}

5>指向结构体的指针
#include <stdio.h>
/*
 1.指向结构体的指针的定义
 struct Student *p;
 
 2.利用指针访问结构体的成员
 1>(*p).成员变量
 2>p->成员变量
 */

int main(int argc, const char * argv[])
{
    struct Student
    {
        int no;
        int age;
    };
    
    //结构体变量
    struct Student stu = {1 ,20};
    
    //指针变量p将来指向struct Student类型的数据
    struct Student *p = &stu;
    
    //指针变量p指向了stu变量;
//    p = &stu;
//    
    p->age = 30;
    
    //第一种方式
    printf("age=%d, no=%d\n",stu.age,stu.no);
    
    //第二种方式
    printf("age=%d, no=%d\n",(*p).age,(*p).no);
    
    //第三种方式
    printf("age=%d, no=%d\n",p->age,p->no);
    return 0;
}

6>嵌套定义
#include <stdio.h>

int main(int argc, const char * argv[])
{
    struct Date
    {
        int year;
        int month;
        int day;
    };
    
    //类型
    struct Student
    {
        int no;//学号
        
        struct Date birthday;//生日
        
        struct Date ruxueDate;//入学日期
        
        //这种写法是错误的
        //struct Student stu;
    };
    
    struct Student stu = {1 , {2000,9,10},{2012,9,10}};
    
    printf("year=%d,month=%d,day=%d\n",stu.birthday.year,stu.birthday.month,stu.birthday.day);
    return 0;
}

3.枚举类型

//枚举用在有特殊规定使用整数的变量,变量之间依次递增
#include <stdio.h>

int main(int argc, const char * argv[])
{
    //1.定义枚举类型
    enum Season
    {
        spring,
        summer,
        autumn,
        winter
    };
    
    //2.定义枚举变量
    enum Season s = spring;
    enum Season s2 = summer;
    enum Season s3 = autumn;
    enum Season s4 = winter;
    
    printf("%d\n",s);
    printf("%d\n",s2);
    printf("%d\n",s3);
    printf("%d\n",s4);
    return 0;
}

4.数据类型总结

/*
 一、基本数据类型
 1.int
 
 1>long int、long:8个字节  %ld
 2>short int、short:2个字节  %d %i
 3>unsigned int、unsigned:4个字节 %zd
 4>signed int、signed 、int:4个字节 %d %i
 
 2.float\double
 1>float:4个字节  %f
 2>double:8个字节  %f
 
 3.char
 1>1个字节 %c %d
 2>char类型保存在内存中得是它的ASCII值
    ‘A’-->65
 
 二、构造类型
 1.数组
 1>只能由同一种类型的数据组成
 2>定义:数据类型  数组名[元素个数];
 
 2.结构体
 1>可以由不同类型的数据组成
 2>先定义类型,再利用类型定义变量
 
 三、指针类型
 1.变量的定义
 int *p;
 
 2.间接操作变量的值
 int a = 10;
 p = &a;
 *p = 20;
 
 四、枚举类型
 使用场合:当一个变量只允许有几个固定取值时
 
 */






---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

黑马程序员 《ios零基础教程》--全局和局部变量、结构体、枚举 2014-4-2总结 的相关文章

随机推荐

  • MathJax 3 配置和上手渲染数学公式及在Vue中的使用

    mathjax是一个用于latex mathml和ascimath表示法的开源javascript显示引擎 mathjax的3 0版是对mathjax的彻底重写 实现了组件化 可以实现不同需求的定制 使用和配置与mathjax2版本有很大的
  • 【redis】Redis cluster是AP架构还是CP架构?

    最近刚好在看CAP理论 加上之前分析的redis cluster 就在想redis的cluster是什么模式的 AP还是CP 首先还是简单讲下CAP 具体的可见 CAP分别是 强一致性 Consistency 可用性 Availabilit
  • 地址栏参数隐藏

    1 result type 的redirectAction改为chain 但要注意如果是登录方法 权限拦截器中就可能会影响 2 将参数放到作用域中 比如session 注意 1 这样的注释是没用的
  • 网络编程13——epoll事件模型:ET和LT模、掌握实现epoll的ET模式(非阻塞模式

    epoll是linux下多路复用IO select poll 的增强版本 它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率 因为它会复用文件描述符集合来传递结果为不用迫使开发者每次等待事件之前都必须重新准备要被侦听的文
  • shiro认证机制及认证原理

    转自 shiro认证机制 认证原理 下文笔者将讲述shiro的认证机制及认证原理 如下所示 Shiro认证 验证用户身份的过程 在认证过程中 用户需要提交实体信息 Principals 和凭据信息 Credentials 以检验用户是否合法
  • 【玩转PointPillars】Ubuntu18.04上部署nutonomy/second.pytorch

    系统环境 Ubuntu18 04 cuda10 2 GeForce GTX 1650 今天部署的项目虽然名称上叫做second pytorch 实际上是PointPillars的作者fork自SECOND项目 并作了改动之后形成的Point
  • 词法分析器构造工具Flex基础学习

    Flex是一个生成词法分析器的工具 它可以利用正则表达式来生成匹配相应字符串的C语言代码 其语法格式基本同Lex相同 单词的描述称为模式 Lexical Pattern 模式一般用正规表达式进行精确描述 FLEX通过读取一个有规定格式的文本
  • SVN 服务器发送了意外的返回值(405 Method Not Allowed),在响应 “MKCOL” 的请求

    先转载一段网上说的解决方法 svn 405 Method Not Allowed 在响应 MKCOL 的请求 I managed to solve the problem Delete the parent s directory of t
  • jupyter lab的目录调整及默认浏览器设置为chrome

    Jupyter lab 的目录调整及默认浏览器设置为chrome 1 Jupyter 默认目录调整 首先要找到jupyter生成的配置文件 jupyter notebook config py 如果没有 在 anaconda prompt
  • 在Anaconda中快速安装OpenCV for Python

    一 下载和安装Anaconda Anaconda下载地址 Anaconda Individual EditionAnaconda s open source Individual Edition is the easiest way to
  • 【吐血整理】java程序员推荐轻薄笔记本

    正文 在写这个文章之前 我花了点时间 自己臆想了一个电商系统 基本上算是麻雀虽小五脏俱全 我今天就用它开刀 一步步剖析 我会讲一下我们可能会接触的技术栈可能不全 但是够用 最后给个学习路线 Tip 请多欣赏一会 每个点看一下 看看什么地方是
  • kali Linux自带firefox ESR设置代理

    1 打开kali的火狐浏览器 找到右上角的 三个杠 在点击 preferences 2 general gt network proxy gt setting 3 打开靶场和burp suite工具 注意火狐浏览器的代理是启动状态 靶场地址
  • 双写绕过的原理

    可以看到代码对key进行了过滤 那怎么办呢 可以构造kekeyy 当key被过滤掉时 剩下的字符自动拼接在一起 就形成了key 所以说 这样就可以拿下flag了
  • 梯度下降(学习笔记)

    应用 梯度下降法 Gradient Descent 又称最速下降法 是迭代法的一种 可用于求解机器学习算法的模型参数 即无约束优化问题 具体来讲可用来求解损失函数的最小值 也可求解最小二乘问题 分类 批量梯度下降 BGD 使用全部样本构建了
  • 职场大佬常用工具:Baklib,一款个人知识笔记管理神器

    又到了大家喜爱的好用工具推荐环节 今天我要给大家推荐一款个人知识笔记管理神器 不出你们所料 它就是Baklib 言归正传那Baklib究竟能干啥呢 引用官网的一句话来说 Baklib工具可以将大家日常工作学习中 存储到电脑 云盘上的文档 知
  • 06makefile学习之三个自动变量($@,$^,$<),模式规则和静态模式规则

    06makefile学习之三个自动变量 lt 和模式规则 以下为相关makefile的学习文章 01makefile学习之GCC编译的四个阶段 带编译阶段 汇编阶段 S c的区别 02makefile学习之makefile的基本原则 03m
  • Oracle存储过程处理大批量数据性能测试

    通过此次的大批量数据性能测试 还会间接的给大家分享一个知识点 Oracle存储过程如何处理List集合的问题 废话不多说了 老规矩直接上代码 首先要做的 想必大家应该猜到了 建表 create table tab 1 id varchar
  • linux内核中打印栈回溯信息 - dump_stack()函数分析

    简介 当内核出现比较严重的错误时 例如发生Oops错误或者内核认为系统运行状态异常 内核就会打印出当前进程的栈回溯信息 其中包含当前执行代码的位置以及相邻的指令 产生错误的原因 关键寄存器的值以及函数调用关系等信息 这些信息对于调试内核错误
  • 使用matlab修改单张或多张图像大小

    使用matlab修改单张或多张图像大小 版权声明 本文为CSDN博主 berlinpand 的原创文章 遵循 CC 4 0 BY SA 版权协议 转载请附上原文出处链接及本声明 原文链接 https blog csdn net berlin
  • 黑马程序员 《ios零基础教程》--全局和局部变量、结构体、枚举 2014-4-2总结

    a href http edu csdn net target self ASP Net Unity开发 a a href http edu csdn net target self Net培训 a 期待与您交流 前几天出差有事儿没学习 今