有關數組的運算:
首先我們分析一下sizeof()和strlen();
sizeof()功能:計算數據空間的字節數
當數組名單獨放進sizeof()裏表示整個數組。
strlen()計算字符數組的字符數,以"\0"爲結束判斷,不計算爲'\0'的數組元素。
1.一位數組的計算
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a[] = {1,2,3,4}; //一位數組
printf("%d\n",sizeof(a)); // 16 a表示整個數組
printf("%d\n",sizeof(a+0)); // 4 a+0 表示數組首元素地址,int型佔4個字節
printf("%d\n",sizeof(*a)); // 4 *a 表示數組首元素, 1 int型
printf("%d\n",sizeof(a+1)); // 4 a+1 表示數組第二個元素的地址
printf("%d\n",sizeof(a[1])); // 4 a[1]表示數組的第二個元素
printf("%d\n",sizeof(&a)); // 4 a表示整個數組,&a表示數組的地址(與數組首元素地址相同,但含義不同)
printf("%d\n",sizeof(&a+1)); // 4 同上 &a+1表示數組後的的地址
printf("%d\n",sizeof(&a[0])); // 4 &a[0]表示首元素地址
printf("%d\n",sizeof(&a[0]+1));// 4 &a[0]+1表示第二個元素地址
return 0;
}
2.字符數組的計算:
註釋:strlen()會默認()內爲一個地址,從此地址開始找字符,直到找到’\0‘,就停止
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <string.h>
int main()
{
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f',};
printf("%d\n",sizeof(arr)); // 6 arr 表示整個數組,char型一個字節,6個元素
printf("%d\n",sizeof(arr+0)); // 4 arr+1 表示數組第二個元素的地址
printf("%d\n",sizeof(*arr)); // 1 *arr 表示第一個元素,char型
printf("%d\n",sizeof(arr[1])); // 1 arr[1] 表示第一個元素,char型
printf("%d\n",sizeof(&arr)); // 4 數組地址的地址(與數組首元素地址相同,但含義不同)
printf("%d\n",sizeof(&arr+1)); // 4 數組地址的下一個地址
printf("%d\n",sizeof(&arr[0]+1)); // 4 數組第二個元素的地址
printf("%d\n",strlen(arr)); // X arr表示素組,但strlen需要有'\0'才能停下來
printf("%d\n",strlen(arr+0)); // X arr+1表示數組首地址,同上
// printf("%d\n",strlen(*arr)); // strlen 是從地址開始的地方查找字符,知道找到'\0'
//printf("%d\n",strlen(arr[1]));
printf("%d\n",strlen(&arr)); // X &arr表示數組的地址
printf("%d\n",strlen(&arr+1)); // X &arr表示數組後的地址
printf("%d\n",strlen(&arr[0]+1)); // X &arr[0]+1 數組的第二個元素
return 0;
}
3. 指針型數組:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <string.h>
int main()
{
char *p = "abcdef";
printf("%d\n",sizeof(p)); // 4 p表示數組首元素的地址
printf("%d\n",sizeof(p+1)); // 4 p+1表示數組第二個元素的地址
printf("%d\n",sizeof(*p)); // 1 *p表示數組首元素
printf("%d\n",sizeof(p[0])); // 1 p[0]表示數組首元素
printf("%d\n",sizeof(&p)); // 4 &p表示數組首元素地址的地址
printf("%d\n",sizeof(&p+1)); // 4 &p表示數組首元素地址的下一個地址
printf("%d\n",sizeof(&p[0]+1)); // 4 &p[0]+1表示數組第二個元素地址
printf("%d\n",strlen(p)); // 6 p表示數組首元素地址,從此開始查找到第七個元素碰到了'\0'結束減1
printf("%d\n",strlen(p+1)); // 5 p+1表示數組第二個元素地址,從此處開始查找
//printf("%d\n",strlen(*p)); // *p表示第一個元素。
//printf("%d\n",strlen(p[0])); // p[0]表示第一個元素
//printf("%d\n",strlen(&p)); // &p表示數組首元素地址的地址
// printf("%d\n",strlen(&p+1)); // X &p表示數組首元素地址的下一個地址
printf("%d\n",strlen(&p[0]+1)); // 5 &p[0]+1表示數組第二個元素地址
return 0;
}
4. 二維數組:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a[3][4] = {0};
printf("%d\n",sizeof(a)); // 48 a 表示整個數組
printf("%d\n",sizeof(a[0][0])); // 4 a[0][0]表示數組第一個元素
printf("%d\n",sizeof(a[0])); // 16 a[0]表示二維數組的第一行
printf("%d\n",sizeof(a[0]+1)); // 4 a[0]+1表示二維數組的第一行第二個元素的地址
printf("%d\n",sizeof(a+1)); // 4 a+1表示數組第二個元素的地址
printf("%d\n",sizeof(&a[0]+1)); // 4 &a[0]+1表示二維數組的第一行地址第二個元素的地址(&a[0]表示第一行地址)
printf("%d\n",sizeof(*a)); // 16 *a 表示數組的第一行元素
printf("%d\n",sizeof(a[3])); // 16 a[3]表示數組的第三行元素
return 0;
}
結論:數組名代表整個數組只有兩種情況:
1. 數組名單獨放在sizeof()中.
2. &數組名,這裏數組名錶示整個數組,而”&數組名“表示整個數組的地址與數組首元素地址值相同但含義不同
例: int arr[10] = {0};
其中:假設:&arr和&arr[0]值上都等於0x00000000
但: &arr+1 = 0x00000028
而&arr[0]+1 = 0x00000004