c代码 双指针改为获取指针_C中的指针到指针或双指针

c代码 双指针改为获取指针

In this tutorial you will learn about pointer to pointer or double pointer in C.

在本教程中,您将学习C中的指针或双指针

Pointer is used to store memory address of variable. Double pointer is used to store memory address of any other pointer.

指针用于存储变量的内存地址。 双指针用于存储任何其他指针的内存地址。

Let’s try to understand this by one example.

让我们尝试通过一个例子来理解这一点。

Also Read: void pointer in C Also Read: C Function Pointer

另请参见 C中的 void指针 也请阅读: C函数指针

C中的指针到指针或双指针 (Pointer to Pointer or Double Pointer in C)

c代码 双指针改为获取指针_C中的指针到指针或双指针

As you can see in above example that p1 is a pointer as it holds memory address of variable a and p2 is double pointer or pointer to pointer as it holds memory address of pointer p1.

如您在上面的示例中看到的那样, p1是指针,因为它持有变量a的内存地址,而p2是双指针或指向指针的指针,因为它持有指针p1的内存地址。

What will be the output if we try to print the values of these 3 variables?

如果我们尝试打印这三个变量的值,输出将是什么?

printf(“%d”, p1): 5000 (address of a)

printf(“%d”,p1): 5000( a的地址)

printf(“%d”, *p1): 65 (value of a)

printf(“%d”,* p1): 65( a的值)

printf(“%d”, p2): 6000 (address of p1)

printf(“%d”,p2): 6000( p1的地址)

printf(“%d”, *p2): 5000 (address of a)

printf(“%d”,* p2): 5000( a的地址)

printf(“%d”, **p2): 65 (value of a)

printf(“%d”,** p2): 65( a的值)

Below program will show you how to use double pointer in C language.

下面的程序将向您展示如何在C语言中使用双指针。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int main()
{
    int a, *p1, **p2;
    a=65;
    p1=&a;
    p2=&p1;
    printf("a = %d\n", a);
    printf("address of a = %d\n", &a);
    printf("p1 = %d\n", p1);
    printf("address p1 = %d\n", &p1);
    printf("*p1 = %d\n", *p1);
    printf("p2 = %d\n", p2);
    printf("*p2 = %d\n", *p2);
    printf("**p2 = %d\n", **p2);
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int main ( )
{
     int a , * p1 , * * p2 ;
     a = 65 ;
     p1 =& a ;
     p2 =& p1 ;
     printf ( "a = %d\n" , a ) ;
     printf ( "address of a = %d\n" , & a ) ;
     printf ( "p1 = %d\n" , p1 ) ;
     printf ( "address p1 = %d\n" , & p1 ) ;
     printf ( "*p1 = %d\n" , * p1 ) ;
     printf ( "p2 = %d\n" , p2 ) ;
     printf ( "*p2 = %d\n" , * p2 ) ;
     printf ( "**p2 = %d\n" , * * p2 ) ;
     return 0 ;
}

Output

输出量

a = 65 address of a = 2686744 p1 = 2686744 address p1 = 2686740 *p1 = 65 p2 = 2686740 *p2 = 2686744 **p2 = 65

a = 65个 地址,a = 2686744 p1 = 2686744 地址p1 = 2686740 * p1 = 65 p2 = 2686740 * p2 = 2686744 ** p2 = 65

Comment below if you found anything incorrect or have doubts related to above tutorial.

如果发现任何不正确的内容或与以上教程相关的疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/08/pointer-pointer-double-pointer-c.html

c代码 双指针改为获取指针