#ifndef _TEST_H_
#define _TEST_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#endif // !_TEST_H所有变量声明
#include "test.h"
#define SIZE 4
int main()
{
int b[] = { 10,20,30,40 };
int* bPtr = b;//定义一个指向数组b的指针
printf("Array b printed with:\nArray index notation\n");//数组索引符号
size_t i = 0;
for (i = 0; i < SIZE; i++)
{
printf("b[%u] = %d\n", i, b[i]);
}
puts("");
printf("Pointer/offset notation where\n");//指针抵消符号
printf("the Poiner is the array name\n");//指针是数组名
size_t offset = 0;
for (offset = 0; offset < SIZE; offset++)
{
printf("*(b + %u) = %d\n", offset, *(b + offset));
}
puts("");
puts("Pointer index notation");//指针索引符号
for (i = 0; i < SIZE; i++)
{
printf("bPtr[%u] = %d\n", i, bPtr[i]);
}
puts("\nPointer/offset notation");
for (offset = 0; offset < SIZE; offset++)
{
printf("*(bPtr + %u) = %d\n", offset, *(bPtr + offset));
}
return 0;
}
C语言中,指针和数组的联系是极为密切的,它们经常互换使用。
- 一个数组名可以看做是一个常量指针,指针可以用于任何涉及数组下标的操作。
- 偏移量:下面表达式中的3代表针对一个指针的偏移量
- *(bPtr + 3)
- Tips:括号很重要,一旦缺少圆括号,表达式会将3与表达式*bPtr的值相加(假设bPtr指向数组的起始位置,则表示3和b[0]相加)。
- &b[3]也可以写成 bPtr + 3
- 数组本身可以被当做一个指针,在指针算数运算中使用
- *(b + 3)
- 也可以表示素组元素 b[3]
- 一般而言,所有带下标的数组表达式都可以写成指针和偏移量的表达形式。