数据结构-指针

指针用于指向存储在计算机内存中任何位置的值的地址。获取存储在该位置的值称为解除引用指针。指针提高了重复过程的性能,例如:

  • 遍历字符串
  • 查找表
  • 控制表
  • 树结构

指针详细信息

  • 指针算术:指针中有四个算术运算符:++--+-
  • 指针数组:可以定义数组以容纳多个指针。
  • 指针的指针:C语言允许指针的指针等等。
  • 将指针传递给C语言的函数:通过引用或地址传递参数使得被调用函数可以在调用函数中更改传递的参数。
  • 从C语言函数返回指针:允许函数返回指向局部变量,静态变量和动态分配内存的指针。

指针示例程序

#include <stdio.h>  

int main()
{
    int a = 5;
    int *b;
    b = &a;

    printf("value of a = %d\n", a);
    printf("value of a = %d\n", *(&a));
    printf("value of a = %d\n", *b);
    printf("address of a = %u\n", &a);
    printf("address of a = %d\n", b);
    printf("address of b = %u\n", &b);
    printf("value of b = address of a = %u", b);
    system("pause");
    return 0;
}

C

执行上面示例代码,得到以下结果 -

value of a = 5
value of a = 5
value of a = 5
address of a = 13630708
address of a = 13630708
address of b = 13630696
value of b = address of a = 13630708

Shell

指针的指针示例程序

#include <stdio.h>  

int main()
{
    int a = 5;
    int *b;
    int **c;
    b = &a;
    c = &b;
    printf("value of a = %d\n", a);
    printf("value of a = %d\n", *(&a));
    printf("value of a = %d\n", *b);
    printf("value of a = %d\n", **c);
    printf("value of b = address of a = %u\n", b);
    printf("value of c = address of b = %u\n", c);
    printf("address of a = %u\n", &a);
    printf("address of a = %u\n", b);
    printf("address of a = %u\n", *c);
    printf("address of b = %u\n", &b);
    printf("address of b = %u\n", c);
    printf("address of c = %u\n", &c);
    system("pause");
    return 0;
}

C

执行上面示例代码,得到以下结果 -

value of a = 5
value of a = 5
value of a = 5
value of a = 5
value of b = address of a = 16252636
value of c = address of b = 16252624
address of a = 16252636
address of a = 16252636
address of a = 16252636
address of b = 16252624
address of b = 16252624
address of c = 16252612

//更多请阅读:https://www.yiibai.com/data_structure/data-structure-pointer.html
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

智慧浩海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值