C 程序显示指针到指针之间的关系


在 C 编程语言中,指向指针或双指针是一个保存另一个指针地址的变量。

声明

以下是指向指针的声明 -

datatype ** pointer_name;

例如,int **p;

此处,p 是指向指针的指针。

初始化

‘&’用于初始化。

例如,

int a = 10;
int *p;
int **q;
p = &a;

访问

解引用运算符 (*) 用于访问

示例程序

以下是双指针的 C 程序 -

 在线演示

#include<stdio.h>
main ( ){
   int a = 10;
   int *p;
   int **q;
   p = &a;
   q = &p;
   printf("a =%d ",a);
   printf(" a value through pointer = %d", *p);
   printf(" a value through pointer to pointer = %d", **q);
}

输出

当执行上述程序时,它生成以下结果 -

a=10
a value through pointer = 10
a value through pointer to pointer = 10

示例

下面,考虑另一个 C 程序,该程序展示指向指针之间的关系。

 在线演示

#include<stdio.h>
void main(){
   //Declaring variables and pointers//
   int a=10;
   int *p;
   p=&a;
   int **q;
   q=&p;
   //Printing required O/p//
   printf("Value of a is %d
",a);//10//    printf("Address location of a is %d
",p);//address of a//    printf("Value of p which is address location of a is %d
",*p);//10//    printf("Address location of p is %d
",q);//address of p//    printf("Value at address location q(which is address location of p) is %d
",*q);//address of a//    printf("Value at address location p(which is address location of a) is %d
",**q);//10// }

输出

当执行上述程序时,它生成以下结果 -

Value of a is 10
Address location of a is 6422036
Value of p which is address location of a is 10
Address location of p is 6422024
Value at address location q(which is address location of p) is 6422036
Value at address location p(which is address location of a) is 10

更新日期: 2021 年 3 月 11 日

260 次浏览

开启您的 职业生涯

完成课程后获得认证

开始学习
广告