1. Basic Concepts of Arrays
(1) Definition of an array
- int a[] = {1, 2};
- int b[100] = {1, 3};
int c[200] = {0}; //When compiling, it has been determined that all values are zero
For a one-dimensional array C, it is specified:
- c is the address of the first element of the array c+1 step size 4 bytes
- &c is the address of the entire array //&c+1 step size 200*4
(2) Array type
- typedef int (MyArrayType)[5]; //Defines a data type array data type
- Type Essence: Aliases for Fixed Size Memory Blocks
code
#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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
(3) Define an array pointer variable
Method 1 (using array type * )
#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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
Method 2 (declare an array pointer type)
#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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
Method 3 (directly define an array pointer variable pointing to an array)
#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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
2. The nature of multidimensional arrays
- The essence of a multidimensional array name is an array pointer, and the step size is the length of one dimension
- (a+i) represents the address secondary pointer of the entire i-th row
- *(a+i) represents the address of the first element of the i-th row of the 1-level pointer
- *(a+i) + j ===> & a[i][j]
- ( (a+i) + j) ===>a[i][j] element value
- 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)
Multidimensional array as function parameter
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]);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
3. Equivalence relation
- 一位数组 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");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133