C/C++学习之C提高----C数组的定义、类型、定义数组指针变量、多维数组的本质、多维数组做函数参数、将两个数组中的字符串copy到第三个数组

来源:互联网 发布:文本数据挖掘 编辑:程序博客网 时间:2024/05/17 21:55

1.数组的基本概念

(1)数组的定义

  • int a[] = {1, 2};
  • int b[100] = {1, 3};
  • int c[200] = {0}; //编译的时候 就已经确定 所有的值 为零

  • 对一维数组 C规定:

  • c是数组首元素的地址 c+1 步长 4个字节
  • &c 是整个数组的地址 //&c+1 步长 200*4

(2)数组类型

  • typedef int (MyArrayType)[5]; //定义了一个数据类型 数组数据类型
  • 类型本质:固定大小内存块的别名

代码

#define  _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>void main(){    typedef int(MyArrayType)[5]; //定义了一个数据类型  数组数据类型    int i = 0;    MyArrayType  myArray;  //int myArray[5];    for (i = 0; i < 5; i++)    {        myArray[i] = i + 1;    }    for (i = 0; i < 5; i++)    {        printf("%d ", myArray[i]);    }    printf("\n");    printf("myArray代表数组首元素的地址 myArray:%d myArray+1:%d \n", myArray, myArray + 1);    printf("&myArray代表整个数组的地址 &myArray:%d &myArray+1:%d \n", &myArray, &myArray + 1);    printf("hello...\n");    system("pause");    return;}

这里写图片描述

(3)定义数组指针变量

方法一(用数组类型 * )

#define  _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>void main(){    char *Myarray[] = { "1111", "33333", "aaaa" }; //指针数组    //数组指针  用一个指针来指向一个数组    typedef int(MyArrayType)[5]; //定义了一个数据类型  数组数据类型    int i = 0;    MyArrayType  myArray;  //int myArray[5]; //用类型定义变量     MyArrayType *pArray; //定义一个指针变量 这个指针变量指向一个数组    {        int myArray2[5]; //相当于一级指针        pArray = &myArray2; //相当于2级指针        for (i = 0; i < 5; i++)        {            (*pArray)[i] = i + 1;        }        for (i = 0; i < 5; i++)        {            printf("%d ", (*pArray)[i]);        }    }    printf("hello...\n");    system("pause");    return;}

这里写图片描述

方法二(声明一个数组指针类型)

#define  _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>void main(){    //定义声明一个数组指针类型    typedef int(*PArrayType)[5];    PArrayType pArray;  //告诉编译器 给我分配一个指针变量    int c[5];    int i = 0;    pArray = &c;    for (i = 0; i < 5; i++)    {        (*pArray)[i] = i + 1;    }    for (i = 0; i < 5; i++)    {        printf("%d ", (*pArray)[i]);    }    printf("hello...\n");    system("pause");    return;}

方法三(直接定义一个指向数组的数组指针变量)

#define  _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>void main(){    int(*pMyArray)[5]; //直接定义一个指向数组的 数组指针变量    int c[5];    int i = 0;    pMyArray = &c;    for (i = 0; i < 5; i++)    {        (*pMyArray)[i] = i + 1;    }    for (i = 0; i < 5; i++)    {        printf("%d ", (*pMyArray)[i]);    }    printf("hello...\n");    system("pause");    return;}

2.多维数组的本质

  • 多维数组名的本质是一个数组指针,步长是一维的长度
  • (a+i) 代表是整个第i行的地址 二级指针
  • *(a+i) 代表 1级指针 第i行首元素的地址
  • *(a+i) + j ===> & a[i][j]
  • ( (a+i) + j) ===>a[i][j]元素的值
  • a[i][j] <=== >((a+i) + j)
  • a[i] ===> a[0+i] ==> *(a+i);
  • a[i][j] ===> a[0+i][j] ==> (a+i)[j] ===> (a+i)[0 + j] ==> ((a+i) + j)

多维数组做函数参数

void printArray01(int a[3][5]) {    int i, j, tmp = 0;    for (i=0; i<3; i++)    {        for (j=0; j<5; j++)        {            printf("%d ", a[i][j]);        }    }}void printArray02(int a[][5]) {    int i, j, tmp = 0;    for (i=0; i<3; i++)    {        for (j=0; j<5; j++)        {            printf("%d ", a[i][j]);        }    }}void printArray03( int (*b)[5]) {    int i, j, tmp = 0;    for (i=0; i<3; i++)    {        for (j=0; j<5; j++)        {            printf("%d ", b[i][j]);        }    }}

3.等价关系

  • 一位数组 char a[30]=====>等价于 指针 char*
  • 指针数组 char *a[30]=====>等价于 指针的指针 char **a
  • 二维数组 char a[10][30]=====>等价于 数组的指针 char (*a)[30]

4.数组的结束标志

#define  _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>void main(){    int inum = 0;    int pos = 0;    int a[10];    int i = 0;    //指针数组  自我结束能力    char*   c_keyword[] = {        "while",        "case",        "static",        "do",        '\0'    };    char*   c_keyword2[] = {        "while",        "case",        "static",        "do",        0    };    char*   c_keyword3[] = {        "while",        "case",        "static",        "do",        NULL    };    for (i = 0; c_keyword[i] != NULL; i++)    {        printf("%s\n", c_keyword[i]);    }    printf("\n....\n");    for (i = 0; c_keyword2[i] != NULL; i++)    {        printf("%s\n", c_keyword2[i]);    }    printf("\n....\n");    for (i = 0; c_keyword3[i] != NULL; i++)    {        printf("%s\n", c_keyword3[i]);    }    system("pause");}

这里写图片描述

5.将两个数组中的字符串copy到第三个数组中

  • 要求:
  • char *p1[] = { “aaaa”, “ddddd”, “bbbbbb” };
  • char buf2[10][30] = { “1111”, “6666”, “33333” };
  • char **p3 = NULL;
  • 将p1和buf2中的字符串copy到p3,并排序打印后释放空间。

(1)代码

#define  _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>int sort(char **myp1 /*in*/, int num1, char (*myp2)[30], int num2, char ***myp3, int *num3){    int i = 0, j = 0, k= 0;    int  tmplen = 0;    char **p3 = NULL;    char *tmpP = NULL;    p3 = (char **)malloc( (num1 + num2) * sizeof(char *)  ); //里面装的是指针    if (p3 == NULL)     {        return -1;    }    for (i=0; i<num1; i++)    {        tmplen= strlen(myp1[i]) + 1;        p3[i] = (char *)malloc( tmplen * sizeof(char)) ;        if (p3[i] == NULL)        {            return -2;        }        strcpy(p3[i], myp1[i]);    }    for (j=0; j<num2; j++, i++)    {        tmplen = strlen(myp2[j]) + 1;        p3[i] = (char *)malloc (tmplen * sizeof(char));        if (p3[i] == NULL)        {            return -3;        }        strcpy(p3[i], myp2[j]);    }    tmplen = num1 + num2;    //排序    for (i=0; i<tmplen; i++)    {        for (j=i+1; j<tmplen; j++)        {            if ( strcmp(p3[i], p3[j]) > 0 )            {                tmpP = p3[i];                p3[i] = p3[j];                p3[j] = tmpP;            }        }    }    //间接赋值    *num3 = tmplen;    *myp3 = p3;    return 0;}void sortFree01(char **p, int len){    int i = 0;    if (p == NULL)    {        return ;    }    for (i=0; i<len ; i++)    {        free(p[i]);    }    free(p);}void sortFree02(char ***myp, int len) //把二级指针指向二维内存释放掉,,同时间接的修改了实参的值{    int i = 0;    char **p = NULL;    if (myp == NULL)    {        return ;    }    p  = *myp; //还原成二级指针    if (p == NULL)    {        return ;    }    for (i=0; i<len ; i++)    {        free(p[i]);    }    free(p);    //myp 是实参的地址    *myp = NULL; //间接赋值是指针存在的最大意义}int  main(){    int ret = 0;    char *p1[] = { "aaaa", "ddddd", "bbbbbb" };    char buf2[10][30] = { "1111", "6666", "33333" };    char **p3 = NULL;    int len1, len2, len3, i = 0;    len1 = sizeof(p1)/sizeof(*p1);    len2 = 3;    ret = sort(p1, len1, buf2, len2, &p3, &len3);    if (ret != 0)    {        printf("func sort() err:%d \n", ret);        return ret;    }    for (i=0; i<len3; i++)    {        printf("%s\n", p3[i]);    }    //sortFree01(p3, len3);    sortFree02(&p3, len3);    printf("hello...\n");    system("pause");    return ret;}

(2)运行结果

这里写图片描述

阅读全文
0 0
原创粉丝点击