有人可以解释指向指针的指针是如何工作的吗? [英] Can someone explain how pointer to pointer works?

查看:16
本文介绍了有人可以解释指向指针的指针是如何工作的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白指向指针的指针是如何工作的.有没有办法在不使用指针的情况下做同样的工作?

I don't really understand how the pointer to pointer works. Any way to do the same work without using pointer to pointer?

struct customer
{
    char name[20];
    char surname[20];
    int code;
    float money;
};
typedef struct customer customer;
void inserts(customer **tmp)
{
    *tmp = (customer *)malloc(sizeof(customer));
    puts("Give me a customer name, surname code and money");
    scanf("%s %s %d %f", (*tmp)->name, (*tmp)->surname, &(*tmp)->code, &(*tmp)->money);
}

推荐答案

Pointers to Pointers 101

假设您有一个 int 变量 x:int x;.

Lets say you have an int variable x: int x;.

  • 一部分内存将分配给 x,足够容纳一个 int.
  • 分配给 x 的内存在进程内存映射中有一个地址.
  • 要查看 x 在内存中的地址,使用:
    printf("x 的内存地址 = %p ", (void*)&x);
  • &x 表示 x 的地址.
  • 要查看存储在 x 中的整数值,请使用:
    printf("x=%d ", x);
  • x 可以only 在它的存在范围内直接操作.例如,它可以在声明它的函数中进行操作:
    x = 42;
  • 在其定义范围之外,如果 x 的内存地址已知,则可以对其值进行操作.
  • 如果将 x 的值(即:42)传递给函数(x),则该函数无法操作 x 的值>.
  • 如果将 x 的地址传递给函数 (&x),则该函数可以操作 x 的值.
  • A portion of memory will be assigned to x large enough to hold an int.
  • The memory assigned to x has an address in the process memory map.
  • To see the address where x is located in memory, use:
    printf("x's memory address = %p ", (void*)&x);
  • &x means address of x.
  • To see the integer value stored in x, use:
    printf("x=%d ", x);
  • x can only be manipulated directly within it's scope of existence. For example, it can be manipulated within the function in which it is declared:
    x = 42;
  • Outside its scope of definition, the value of x can be manipulated if its memory address is known.
  • If the value of x (ie: 42) is passed to a function(x), that function cannot manipulate the value of x.
  • If the address of x is passed to a function(&x), that function can manipulate the value of x.

现在,假设您有一个指针变量 p,它假定它指向一个 int:int *p;

Now, lets say that you have a pointer variable p that assumes it points to an int: int *p;

  • 一部分内存将分配给 p 足够大的内存地址.
  • 分配给 p 的内存在进程内存映射中有一个地址.
  • &p 表示地址 p.
  • 要查看p在内存中的地址,使用:
    printf("p's memory addr = %p ", &p);
  • 要查看 p 指向的地址,请使用:
    printf("p 指向的地址:%p ", p);
  • 要查看所指的整数,请使用:
    printf("int = %d ", *p);
  • p 的值是内存中的一个地址;并且 p 可以设置为指向任何地址,无论该地址是否实际存在于进程内存映射中.
  • 对象的地址是指向该对象的指针.因此,要使 p 指向 x:
    p = &x
  • p 指向的任何东西都可以通过指针类型(p 的 int 类型)引用.
  • 分配给 p 的内存量会有所不同,具体取决于机器的架构;以及它如何表示地址.
  • p 可以only 在它的存在范围内被直接操作.例如,它可以在声明它的函数中进行操作:
    p = &x;p = NULL
  • 在它存在的范围之外,如果 p 的值是已知的,它的内存地址是可以被操纵的.
  • 如果 p 的值(一个地址)被传递给一个函数(p),这个函数就不能操纵 'p' 来改变它指向的位置.
  • 如果将 p 的地址传递给函数 (&p),则该函数可以操作 p 指向的内容.
  • A portion of memory will be assigned to p large enough to hold a memory address.
  • The memory assigned to p has an address in the process memory map.
  • &p means address of p.
  • To see the address where p is located in memory, use:
    printf("p's memory addr = %p ", &p);
  • To see the address where p is pointing, use:
    printf("Address where p is pointing: %p ", p);
  • To see the alleged integer being pointed to, use:
    printf("int = %d ", *p);
  • The value of p is an address in memory; and p can be set to point to any address, whether or not that address actually exists in the process memory map.
  • The address of an object is a pointer to that object. Hence, To cause p to point to x:
    p = &x
  • Whatever p is pointing at can be referred to by the pointer type (type int for p).
  • The amount of memory assigned to p will vary, depending on the architecture of the machine; and how it represents an address.
  • p can only be manipulated directly within it's scope of existence. For example, it can be manipulated within the function in which it is declared:
    p = &x; or p = NULL
  • Outside its scope of existence, the value of p can be manipulated if its' memory address is known.
  • If the value of p (an address) is passed to a function(p), that function cannot manipulate 'p' to change where it points.
  • If the address of p is passed to a function(&p), that function can manipulate what p points to.

现在,假设您有一个指向指针变量 pp 的指针,该变量假定它指向一个指向int"的指针: int **pp; ...or: void inserts(customer **tmp) :

Now, lets say that you have a pointer to a pointer variable pp that assumes it points to a pointer to an 'int': int **pp; ...or: void inserts(customer **tmp) :

  • 指针的地址是指向指针的指针.
  • ...

回到问题

问:有什么方法可以不用指针来做同样的工作?

没有.假设如下:

void inserts(customer **tmp);

...
   {
   customer cust;
   custPtr  custPtr = &cust;

   inserts(&custPtr);
   ... 

inserts() 函数需要一个指针的地址,以便操作 custPtr 指向的位置.

The inserts() function requires the address of a pointer in order to manipulate where custPtr points.

如果相反:

void inserts2(customer *tmp);

...
   {
   customer cust;
   custPtr  custPtr = &cust;

   inserts2(custPtr);
   ... 

insert2() 将获得custPtr 值的副本,即cust 的地址.因此,insert2() 可以修改 cust 的值,但不能改变 custPtr 指向的位置.

The insert2() would get a copy of the value of custPtr, which is the address of cust. Hence, insert2() could modify the value(s) of cust, but could not change where custPtr is pointing.

这篇关于有人可以解释指向指针的指针是如何工作的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆