Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么main()中声明的指针没有更改?_C++_Pointers - Fatal编程技术网

C++ 为什么main()中声明的指针没有更改?

C++ 为什么main()中声明的指针没有更改?,c++,pointers,C++,Pointers,我知道指针调用,我们在其中传递变量的地址。类似这样: void swap(int *x, int *y) { int temp; temp = *x; /* save the value at address x */ *x = *y; /* put y into x */ *y = temp; /* put x into y */ return; } swap(&a, &b); 还有通过引用调用,在这两种方法中,函数中所做的更改都反映在实

我知道指针调用,我们在其中传递变量的地址。类似这样:

void swap(int *x, int *y)
{
   int temp;
   temp = *x; /* save the value at address x */
   *x = *y; /* put y into x */
   *y = temp; /* put x into y */

   return;
}

  swap(&a, &b);
还有通过引用调用,在这两种方法中,函数中所做的更改都反映在实际参数的变量中。
但为什么在这种调用情况下传递的实际参数没有改变:

#include <iostream>
using namespace std;
void foo(int* c){
c=c+1;
}
int main()
{
    int a=5;
    int *c=&a;
    cout<<&c<<endl; //0x7ffe1a74f3b0
    foo(c);        
    cout<<*c<<endl;//5
    cout<<&c<<endl;//0x7ffe1a74f3b0
}
#包括
使用名称空间std;
无效foo(int*c){
c=c+1;
}
int main()
{
INTA=5;
int*c=&a;
库特
也可以通过引用调用,在这两种方法中,函数中所做的更改都反映在实际参数的变量中

不过,有一个重要的区别:总是对引用/指向的对象进行更改,而不是对引用/指针本身进行更改(修改引用通常是不可能的)

这就是为什么在
foo
内部分配
c
一个新值对
c
外部
foo
没有影响:传递给函数的指针被复制


如果需要修改指针,则需要通过传递指针引用或指向指针的指针来添加另一级别的取消引用。

根据注释,函数
main
中定义的变量
c
与函数
foo
的参数
c
是不同的变量
要能够修改main的
c
,即修改
c
指针类型保持的地址,则需要将指向
c
的引用或指针传递给函数

下面的示例显示了通过值传递
c
(as
int*
)或通过引用传递
int**
int*&
)之间的区别不要被
int*
是指针类型这一事实所愚弄,这意味着它可以通过引用接收
int
,或者通过值接收
int*
。由于main的
c
int*
而不是
int
,main
c
是通过值传递的

注意调用函数的方式(函数调用中是否需要地址运算符
&
)和每个函数的结果的不同

#include <iostream>
using namespace std;

void foo_int_ptr(int* c)
{
    c=c+1;
}

void foo_int_ptr_ptr(int** c)
{
    *c=*c+1;
}

void foo_int_ptr_ref(int*& c)
{
    c=c+1;
}

int main()
{
    int a=5;
    int *c=&a;
    cout << "&c=" << &c << ", c=" << c << ", *c=" << (c==&a ? std::to_string(*c) : std::string("INVALID PTR")) << endl;
    foo_int_ptr(c);
    cout << "&c=" << &c << ", c=" << c << ", *c=" << (c==&a ? std::to_string(*c) : std::string("INVALID PTR")) << endl;
    foo_int_ptr_ptr(&c);
    cout << "&c=" << &c << ", c=" << c << ", *c=" << (c==&a ? std::to_string(*c) : std::string("INVALID PTR")) << endl;
    foo_int_ptr_ref(c);
    cout << "&c=" << &c << ", c=" << c << ", *c=" << (c==&a ? std::to_string(*c) : std::string("INVALID PTR")) << endl;
}

你对这件事的想法是错误的

int *c = &a; 

这并不意味着c“包含”a的地址,这意味着c是指向a地址的指针。将指针传递给foo()将不会起任何作用。

foo()
的参数是通过值传递的。这意味着
c=c+1
修改foo的参数。就是这样。它对
main()中名为
c
的完全不同的变量没有任何影响
。结束。值是怎么来的?c包含a的addr。指针是某种类型的一部分。
int*
int**
不同,后者与
int***
不同。您通过值传递指针。如果使用
*c=value
分配给它,显然这将反映在调用程序中,但是更改c的值不会有任何效果。更改
c
的值和更改
c
指向的值之间有区别。当调用
foo
时,其参数
c
设置为
a
的地址。然后
c=c+1;
行将
c
设置为当
foo
返回时,它的参数
c
被丢弃,在
main
中定义的不同变量
c
不会受到任何影响。也许你是想写
*c=*c+1;
。@davidscallett不,我的意思是c=c+1;而我是认为它应该更改main中c的地址实际上,
c
确实包含
a
的地址,
c
是指向
a
的指针。但是当传递到
foo
时,
foo
在新指针变量中接收到
a
地址的副本。
int *c = &a;