zoukankan      html  css  js  c++  java
  • C语言基础知识回顾

    一、数据类型

    1.1 基本数据类型

    整型:int, long,unsigned int,unsigned long,long long……

    字符型:char

    浮点型:float, double……

    【例子】

    //no.1
    int a,b,c;
    a = 1;
    b = 2;
    c = a + b;
    
    //no.2
    char s;
    s = ‘a’;
    float f;
    f = 3.1415;
    

    1.2 结构体类型

    定义:用系统已有的不同基本数据类型或者用户自定义的结构型组合成的用户需要的复杂数据类型。

    【例子】

    struct Student{
        int num;
        char name[20];
        int age;
        float score;
    };
    
    struct Student s1,s2;
    s1.num = 101;
    s2.num = 102;
    

    改进:指定新的类型名来代替已有的类型名

    typedef int Integer;
    typedef float Real;
    
    int i,j; ——>Integer i,j;
    float a,b; ——>Real a,b;
    

    使用typedef改进结构体

    typedef struct Student{ 
        int num;
        char name[20];
        int age;
        float score;
    }Student;
    
    Student s1,s2;
    s1.num = 101;
    s2.num = 102;
    

    1.3 指针类型

    一个变量的地址称为该变量的“指针”,专门存放变量地址的一类变量成为“指针变量”

    【例子】

    //no.1
    int *a;
    int b = 0;
    a = &b;
    *a = 1; ——>b = 1;
    
    //no.2
    char *c;
    char d = ‘a’;
    c = &d;
    *c = ‘A’; ——>d = ‘A’;
    
    //no.3
    typedef struct Student{ 
        int num;
        char name[20];
        int age;
        float score;
    }Student;
    
    Student s1;
    Student *s1_p;
    s1_p = &s1;
    s1.age = 23; ——> (*s1_p).age = 23;
                 ——>  s1_p->age = 23;
    

    二、函数

    2.1 被传入函数的参数是否会改变,执行结果是多少,为什么?

    //no.1
    void fun(int num){
        num++;
    }
    
    int a = 1;
    fun(a);
    printf(“%d”,a);
    
    //no.2
    void fun(int &num){
        num++;
    }
    
    int a = 1;
    fun(a);
    printf(“%d”,a);
    
    //no.3
    void fun(int a[]){
        a[0]++;
    }
    
    int a[10];
    a[0] = 1;
    fun(a);
    printf(“%d”,a[0]);
    

    三、动态内存分配

    3.1 使用malloc函数分配空间

    函数原型:void *malloc(unsigned int size);

    函数作用:在内存的动态存储区中分配一个长度为size的连续空间,并返回所分配第一个字节的地址

    float *f = (float *)malloc(4);
    char *c = (char *)malloc(1);
    Student *s1_p = (Student *)malloc( ??);
    

    改进:使用sizeof配合malloc分配

    定义:sizeof是测量类型或者变量长度的运算符

    int num1 = sizeof(float); 
    int num2 = sizeof(char);
    int num3 = sizeof(Student);
    
    float *f = (float *)malloc(sizeof(float));
    char *c = (char *)malloc(sizeof(char));
    Student *s1_p = (Student *)malloc(sizeof(Student));
    

    3.2 使用free函数释放空间

    函数原型:void free(void *p);

    函数作用:释放指针变量p所指向的动态空间,使这部分空间可以被其他变量使用

    float *f = (float *)malloc(sizeof(float));
    char *c = (char *)malloc(sizeof(char));
    Student *s1_p = (Student *)malloc(sizeof(Student));、
    ……//此处省略部分操作
    free(f);
    free(c);
    free(s1_p);
    

    查看更多

  • 相关阅读:
    哪怕我变成一个庸俗女子
    硬盘接口的种类
    HTTPS与SHTTP
    Jquery动态加载页面
    iText简介
    TFS offline issue
    VS2008 调试慢的问题
    One error related to msxml4.dll (0x800C0014)
    46 Run Commands for Frequently Used Areas of Your Windows Computer
    快速启动工具入门——以Launchy为例(二)
  • 原文地址:https://www.cnblogs.com/lastk/p/12451679.html
Copyright © 2011-2022 走看看