• 数组与指针的恩怨


    1、数组的本质

    (1)、一种构造类型,(2)、相同类型的连续分配内存,(3)、数组的大小为sizeof(type)*array_size(模子type[ ]大小),(4)、数组名为数组的第一个元素的首地址 (5)、有时候可以当作指针常量

    2、数组的地址VS数组名

    数组的地址为&a,数组名为a,两者的指针运算也不一样,像&a+1,与a+1 是不同的

    3、指针的本质

    (1)、是一个变量(2)、是占用一定的内存空间(3)、用于保存内存地址的值(4)、可以通过*(钥匙)取得内容 (5)参数传递,丢失长度信息(6)、const左数右指

    4、数组VS指针

    数组:声明编译器自动分配一片连续的内存空间,数组名为指针常量,

    指针:声明时表示只分配容纳4个字节空间的地址,指针为变量

    5、字符串:

    字符数组、字符串(以结束的字符数组)、

    char a[]="aaaaa";分布在栈空间

    char *a="aaaaa";分布在只读存储区

    strlen求取字符串长度

    strcpy与 strncpy表示无限制与有限制,表示拷贝

    strcat与strncat表示无限制与有限制,表示连接

    #include <stdio.h>
    //#include <syslib.h>
    #include <string.h>
    main()
    {
    char d[]="Golden Global";  // 可以运行   堆空间
    //char *d ="Golden Global";   // 不可以运行 只读存储区
    char *s=" View WinIDE Library";
    //clrscr();
    strncpy(d,s,3);
    printf("%s",d);
    }

    6、数组指针与指针数组

    数组指针:本质是指针,类型为数组类型

    typedef int(INT)[len];  //表示定义一个数组类型(也就是定义了一个模子)

    INT a; //定义一个变量

    INT * p;// 表示定义一个数组指针

    等价于int (*p)[len]

    #include<stdio.h>

    int main()
    {
     //数组指针
     int a[3]={1,2,3};
     int *p1=a;
     typedef int(AINT)[3];
     AINT *p2=&a;

     int b[2][2]={{1,2},{3,4}};
     typedef int(MATRIX)[2][2];
     MATRIX *p3=&b;

     int (*p4)[3]=&a;
     
     //指针数组
     char *c[10];
     c[0]="zhao";
     c[1]="chuang";
     printf("%s ",c[0]);
     
     return  0;
    }

    指针数组:本质是数组,内容是字符串

    char * aa[len]={"aa","aa","cc"}

    #include <stdio.h>
    #include <string.h>

    int lookup_keyword(const char* key, const char* table[], const int size)
    {
        int ret = -1;
       
        int i = 0;
       
        for(i=0; i<size; i++)
        {
            if( strcmp(key, table[i]) == 0 )
            {
                ret = i;
                break;
            }
        }
       
        return ret;
    }

    #define DIM(a) (sizeof(a)/sizeof(*a))  //深刻理解*a代表32为的内存地址

    int main()
    {
        const char* keyword[] = {
                "do",
                "for",
                "if",
                "register",
                "return",
                "switch",
                "while",
                "case",
                "static"
        };
        printf("%d %d ",sizeof(keyword),sizeof(*keyword));
    //keyword数组存放32为地址4个字节,*keyword表示第一个元素的指针(地址)
         printf("%s ",*keyword);
        printf("%d ", lookup_keyword("return", keyword, DIM(keyword)));
        printf("%d ", lookup_keyword("main", keyword, DIM(keyword)));
    }

    7、main函数知道少

    #include <stdio.h>
    //argc 表示参数个数。argv表命令,env表示环境变量
    int main(int argc, char* argv[], char* env[])
    {
        int i = 0;
       
        printf("============== Begin argv ============== ");
       
        for(i=0; i<argc; i++)
        {
            printf("%s ", argv[i]);
        }
       
        printf("============== End argv ============== ");
       
        printf(" ");
        printf(" ");
        printf(" ");
       
        printf("============== Begin env ============== ");
       
        for(i=0; env[i]!=NULL; i++)
        {
            printf("%s ", env[i]);
        }
       
        printf("============== End env ============== ");
    }

  • 相关阅读:
    python运行js---execjs 使用
    使用百度文字识别API进行图片中文字的识别
    cpca 使用python提取中文地址描述中的省市区信息
    Android 设备信息获取详解
    Android实现左滑退出Activity(完美封装)
    Postman测试Soap协议接口
    如何使用postman带Token测试接口?
    Postman高级使用——Tests 断言校验返回结果
    Android Service完全解析,关于服务你所需知道的一切(下)
    Android Service完全解析,关于服务你所需知道的一切(上)
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3249185.html
Copyright © 2020-2023  润新知