指针和数组的关系

#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]
  • 一般而言,所有带下标的数组表达式都可以写成指针和偏移量的表达形式。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枳洛淮南✘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值