C 有时还使用双指针通过引用将指针传递给函数
“有时还使用双指针通过引用将指针传递给函数”C 有时还使用双指针通过引用将指针传递给函数,c,pointers,C,Pointers,“有时还使用双指针通过引用将指针传递给函数” 有人能给我解释一下上面的说法吗?引用指向函数到底意味着什么?这意味着你有一个函数,它接受一个指针指针(例如键入int**)。这允许您以通过引用传递指针所允许的方式修改指针(它指向的数据) void change (int *p) {*p = 7;} void Really_Change (int **pp) {*pp = null;} int p = 1; int *pp = &p; // now, pp is pointing to p.
有人能给我解释一下上面的说法吗?引用指向函数到底意味着什么?这意味着你有一个函数,它接受一个指针指针(例如键入
int**
)。这允许您以通过引用传递指针所允许的方式修改指针(它指向的数据)
void change (int *p) {*p = 7;}
void Really_Change (int **pp) {*pp = null;}
int p = 1;
int *pp = &p;
// now, pp is pointing to p. Let's say it has address 0x10;
// this makes a copy of the address of p. The value of &p is still 0x10 (points to p).
// but, it uses that address to change p to 7.
change(&p);
printf("%d\n", p); // prints 7;
// this call gets the address of pp. It can change pp's value
// much like p was changed above.
Really_Change(&pp);
// pp has been set to null, much like p was set to 7.
printf("%d\n", *pp); // error dereference null. Ka-BOOM!!!
因此,正如您可以将指针传递给
int
并更改其值一样,您可以将指针传递给指针并更改其值(这会更改指针指向的内容)。这意味着您有一个函数,它接受指针指针(例如键入int**
)。这允许您以通过引用传递指针所允许的方式修改指针(它指向的数据)
void change (int *p) {*p = 7;}
void Really_Change (int **pp) {*pp = null;}
int p = 1;
int *pp = &p;
// now, pp is pointing to p. Let's say it has address 0x10;
// this makes a copy of the address of p. The value of &p is still 0x10 (points to p).
// but, it uses that address to change p to 7.
change(&p);
printf("%d\n", p); // prints 7;
// this call gets the address of pp. It can change pp's value
// much like p was changed above.
Really_Change(&pp);
// pp has been set to null, much like p was set to 7.
printf("%d\n", *pp); // error dereference null. Ka-BOOM!!!
因此,正如您可以将指针传递到
int
并更改值一样,您可以将指针传递到指针并更改其值(这会更改指针指向的内容)。我相信这个示例可以更清楚地说明:
//Double pointer is taken as argument
void allocate(int** p, int n)
{
//Change the value of *p, this modification is available outside the function
*p = (int*)malloc(sizeof(int) * n);
}
int main()
{
int* p = NULL;
//Pass the address of the pointer
allocate(&p,1);
//The pointer has been modified to point to proper memory location
//Hence this statement will work
*p=10;
//Free the memory allocated
free(p);
return 0;
}
我相信这个例子更清楚:
//Double pointer is taken as argument
void allocate(int** p, int n)
{
//Change the value of *p, this modification is available outside the function
*p = (int*)malloc(sizeof(int) * n);
}
int main()
{
int* p = NULL;
//Pass the address of the pointer
allocate(&p,1);
//The pointer has been modified to point to proper memory location
//Hence this statement will work
*p=10;
//Free the memory allocated
free(p);
return 0;
}
我将尝试用代码和简单的英语解释:)。解释可能会很长,但值得一试 假设我们有一个程序,运行它的main()函数,我们调用另一个函数,该函数接受一个
int
参数
从概念上讲,当您将变量作为参数传递给函数时,可以通过两种方式(粗略地说)传递:通过值传递,或者通过引用传递
“按值”是指给函数一个变量的副本。函数将接收其“内容”(值),但它无法在自己的代码体之外更改实际变量,因为它只获得了一个副本
另一方面,“通过引用”意味着给函数提供变量的实际内存地址。使用它,函数可以找到变量的值,但也可以转到指定的地址并修改变量的内容
在我们的C程序中,“by value”意味着传递int
的副本(仅将int
作为参数),而“by reference”意味着传递指向它的指针
让我们看一个小代码示例:
void foo(int n) {
n = 10;
printf("%d\n", n);
}
int main() {
int n = 5;
foo(n);
printf("%d\n", n);
return 0;
}
这个程序的输出是什么<代码>10 10?没有<代码>10 5!因为我们通过值而不是引用传递了int
的副本,foo()
只修改了其副本中存储的数字,无法访问main()
的副本
现在,如果我们这样做:
void foo(int* n) {
*n = 10;
printf("%d\n", *n);
}
int main() {
int n = 5;
foo(&n);
printf("%d\n", n);
return 0;
}
这次我们通过引用给出了我们的整数:它是实际的内存地址foo()
具有通过访问它在内存中的位置来修改它的全部权限,foo()
和main()
使用相同的副本,因此输出将是10
如您所见,指针是一个引用,。。。而且在记忆中还有一个数字位置。它类似于int
,只是其中包含的数字有不同的解释。可以这样想:当我们通过引用传递int时,就是通过值传递int指针!。因此,相同的按值/按引用逻辑可以应用于指针,即使它们已经是引用
如果我们的实际变量不是一个int,而是一个int引用(指针),我们希望main()
和foo()
共享该引用的相同副本,以便foo()
可以修改它,我们会怎么做?当然,我们需要一个参考资料!指向指针的指针。即:
int n; /* integer */
int* n; /* integer reference(pointer). Stores an int's position in memory */
int** n; /* reference to integer reference, or double pointer.
Stores int*'s memory address so we can pass int*s by reference. */
我希望这是有用的。我将尝试用代码和简单的英语解释:)。解释可能会很长,但值得一试 假设我们有一个程序,运行它的main()函数,我们调用另一个函数,该函数接受一个
int
参数
从概念上讲,当您将变量作为参数传递给函数时,可以通过两种方式(粗略地说)传递:通过值传递,或者通过引用传递
“按值”是指给函数一个变量的副本。函数将接收其“内容”(值),但它无法在自己的代码体之外更改实际变量,因为它只获得了一个副本
另一方面,“通过引用”意味着给函数提供变量的实际内存地址。使用它,函数可以找到变量的值,但也可以转到指定的地址并修改变量的内容
在我们的C程序中,“by value”意味着传递int
的副本(仅将int
作为参数),而“by reference”意味着传递指向它的指针
让我们看一个小代码示例:
void foo(int n) {
n = 10;
printf("%d\n", n);
}
int main() {
int n = 5;
foo(n);
printf("%d\n", n);
return 0;
}
这个程序的输出是什么<代码>10 10?没有<代码>10 5!因为我们通过值而不是引用传递了int
的副本,foo()
只修改了其副本中存储的数字,无法访问main()
的副本
现在,如果我们这样做:
void foo(int* n) {
*n = 10;
printf("%d\n", *n);
}
int main() {
int n = 5;
foo(&n);
printf("%d\n", n);
return 0;
}
这次我们通过引用给出了我们的整数:它是实际的内存地址foo()
具有通过访问它在内存中的位置来修改它的全部权限,foo()
和main()
使用相同的副本,因此输出将是10
如您所见,指针是一个引用,。。。而且在记忆中还有一个数字位置。它类似于int
,只是其中包含的数字有不同的解释。可以这样想:当我们通过引用传递int时,就是通过值传递int指针!。因此,相同的按值/按引用逻辑可以应用于指针,即使它们已经是引用
如果我们的实际变量不是int,而是int引用(指针),我们希望main()
和foo()
共享