在指针前面的&和是做什么?

时间:2022-05-20 20:18:32

When functions are being called I often see the ampersand in front of the pointer in the function parameter.

在调用函数时,我经常在函数参数的指针前面看到&号。

E.g.

如。

int *ptr;
randomFunction(&ptr);

I have done some research and found that this means that the function uses pointers to pointers. Is the & sign in front of a pointer used just to indicate this or does it do something else?

我已经做了一些研究,发现这意味着函数使用指针指向指针。在指针前面的&符号是用来表示这个还是做其他的事情?

4 个解决方案

#1


22  

It's a pointer to the pointer.

& is the reference operator, and can be read as address of. In your example, it will get another pointer, that is the address of the pointer given as it's argument, i.e. a pointer to the pointer.

为引用操作符,可读为在你的例子中,它会得到另一个指针,那是作为参数给出的指针的地址,也就是指向指针的指针。

Look at the following example:

请看下面的例子:

int **ipp;
int i = 5, j = 6, k = 7;
int *ip1 = &i, *ip2 = &j;
ipp = &ip1;

You will get:

你将得到:

在指针前面的&和是做什么?

In the above example, ipp is a pointer to pointer. ipp stores the address of ip1 and ip1 stores the address of i.

在上面的例子中,ipp是指向指针的指针。ipp存储ip1的地址,ip1存储i的地址。

You can check out Pointers to Pointers for more info.

您可以查看指向指针的指针以获取更多信息。

#2


4  

Take a step back. The fundamental rules of pointer operators are:

后退一步。指针操作符的基本规则是:

  • The * operator turns a value of type pointer to T into a variable of type T.
  • 操作符将指向T的类型指针的值转换为类型T的变量。
  • The & operator turns a variable of type T into a value of type pointer to T.
  • &操作符将类型T的变量转换为指向T的类型指针的值。

So when you have

所以当你有

int *ptr;

ptr is a variable of type pointer to int. Therefore *ptr is a variable of type int -- the * turns a pointer into a variable. You can say *ptr = 123;.

ptr是指向int的类型指针的变量,因此*ptr是类型int的变量——*将指针变成变量。你可以说*ptr = 123;

Since ptr is a variable of type pointer to int, &ptr is a value -- not a variable -- of type pointer to pointer to int:

因为ptr是指向int的类型指针的变量,而ptr是指向int的类型指针的值——而不是变量:

int **pp = &ptr;

&ptr is a value of type pointer to pointer to int. pp is a variable of type pointer to pointer to int. *pp is a variable of type pointer to int, and in fact is the same variable as ptr. The * is the inverse of the &.

&ptr是指向int. pp的类型指针的值,pp是指向int的类型指针的变量,*pp是指向int的类型指针的变量,实际上是与ptr相同的变量。*是&的逆。

Make sense?

有意义吗?

#3


1  

It helps to think of "&" this way. int function_name ( &( whatever ) ); You are passing the address of ( whatever ). Whatever can be a number of things: an elementary variable. a function. a structure. a union. an array. You should mentally translate "&" to "take the address of". So your example means : pass a COPY of the address of the address of the variable ptr of type int!

这样想“&”是有帮助的。int function_name(&(随便));您正在传递(任何)的地址。任何东西都可以是一些东西:一个基本变量。一个函数。一个结构。工会。一个数组。你应该在心里把“&”翻译成“take the address of”。因此,您的示例意味着:传递类型为int的变量ptr的地址的副本!

#4


0  

Int *ptr;

&ptr returns the address of a pointer variable ptr. In short, double pointer or int** holds the address of ptr with &ptr.

ptr返回一个指针变量ptr的地址。简而言之,双指针或int**用&ptr保存ptr的地址。

#1


22  

It's a pointer to the pointer.

& is the reference operator, and can be read as address of. In your example, it will get another pointer, that is the address of the pointer given as it's argument, i.e. a pointer to the pointer.

为引用操作符,可读为在你的例子中,它会得到另一个指针,那是作为参数给出的指针的地址,也就是指向指针的指针。

Look at the following example:

请看下面的例子:

int **ipp;
int i = 5, j = 6, k = 7;
int *ip1 = &i, *ip2 = &j;
ipp = &ip1;

You will get:

你将得到:

在指针前面的&和是做什么?

In the above example, ipp is a pointer to pointer. ipp stores the address of ip1 and ip1 stores the address of i.

在上面的例子中,ipp是指向指针的指针。ipp存储ip1的地址,ip1存储i的地址。

You can check out Pointers to Pointers for more info.

您可以查看指向指针的指针以获取更多信息。

#2


4  

Take a step back. The fundamental rules of pointer operators are:

后退一步。指针操作符的基本规则是:

  • The * operator turns a value of type pointer to T into a variable of type T.
  • 操作符将指向T的类型指针的值转换为类型T的变量。
  • The & operator turns a variable of type T into a value of type pointer to T.
  • &操作符将类型T的变量转换为指向T的类型指针的值。

So when you have

所以当你有

int *ptr;

ptr is a variable of type pointer to int. Therefore *ptr is a variable of type int -- the * turns a pointer into a variable. You can say *ptr = 123;.

ptr是指向int的类型指针的变量,因此*ptr是类型int的变量——*将指针变成变量。你可以说*ptr = 123;

Since ptr is a variable of type pointer to int, &ptr is a value -- not a variable -- of type pointer to pointer to int:

因为ptr是指向int的类型指针的变量,而ptr是指向int的类型指针的值——而不是变量:

int **pp = &ptr;

&ptr is a value of type pointer to pointer to int. pp is a variable of type pointer to pointer to int. *pp is a variable of type pointer to int, and in fact is the same variable as ptr. The * is the inverse of the &.

&ptr是指向int. pp的类型指针的值,pp是指向int的类型指针的变量,*pp是指向int的类型指针的变量,实际上是与ptr相同的变量。*是&的逆。

Make sense?

有意义吗?

#3


1  

It helps to think of "&" this way. int function_name ( &( whatever ) ); You are passing the address of ( whatever ). Whatever can be a number of things: an elementary variable. a function. a structure. a union. an array. You should mentally translate "&" to "take the address of". So your example means : pass a COPY of the address of the address of the variable ptr of type int!

这样想“&”是有帮助的。int function_name(&(随便));您正在传递(任何)的地址。任何东西都可以是一些东西:一个基本变量。一个函数。一个结构。工会。一个数组。你应该在心里把“&”翻译成“take the address of”。因此,您的示例意味着:传递类型为int的变量ptr的地址的副本!

#4


0  

Int *ptr;

&ptr returns the address of a pointer variable ptr. In short, double pointer or int** holds the address of ptr with &ptr.

ptr返回一个指针变量ptr的地址。简而言之,双指针或int**用&ptr保存ptr的地址。

相关文章