C++ 返回指针的地址
我尝试使用以下代码返回指针的地址:C++ 返回指针的地址,c++,pointers,C++,Pointers,我尝试使用以下代码返回指针的地址: #include <iostream> using namespace std; int main(){ int x = 5; int *p = &x; //int **pp = &p; cout << &p << endl; return 0; } #包括 使用名称空间std; int main(){ int x=5; int*p=&x; //int**p
#include <iostream>
using namespace std;
int main(){
int x = 5;
int *p = &x;
//int **pp = &p;
cout << &p << endl;
return 0;
}
#包括
使用名称空间std;
int main(){
int x=5;
int*p=&x;
//int**pp=&p;
cout我无法用代码重现您所说的内容:
这是正确的,实际上&p
是&x+4
,在32位体系结构上是正确的,因为它们是在堆栈上分配的。我无法用代码重现您所说的内容:
int x = 5;
int *p = &x; // b stores addres of variabile a
cout << "Adress of x is : " << p <<endl; // printing the adress of a
cout << "Value of x is : " << *p << endl; // printing the variabile stores in a
int **pp = &p; // pp stores thea adress of p
cout <<"Adress of p is" << pp << endl; //printing the adress of p
cout << "Adress of x is" <<*pp << endl; //printing the adress of x
return 0;
这是正确的,实际上&p
是&x+4
,在32位体系结构上是正确的,因为它们是在堆栈上分配的。我无法用代码重现您所说的内容:
int x = 5;
int *p = &x; // b stores addres of variabile a
cout << "Adress of x is : " << p <<endl; // printing the adress of a
cout << "Value of x is : " << *p << endl; // printing the variabile stores in a
int **pp = &p; // pp stores thea adress of p
cout <<"Adress of p is" << pp << endl; //printing the adress of p
cout << "Adress of x is" <<*pp << endl; //printing the adress of x
return 0;
这是正确的,实际上&p
是&x+4
,在32位体系结构上是正确的,因为它们是在堆栈上分配的。我无法用代码重现您所说的内容:
int x = 5;
int *p = &x; // b stores addres of variabile a
cout << "Adress of x is : " << p <<endl; // printing the adress of a
cout << "Value of x is : " << *p << endl; // printing the variabile stores in a
int **pp = &p; // pp stores thea adress of p
cout <<"Adress of p is" << pp << endl; //printing the adress of p
cout << "Adress of x is" <<*pp << endl; //printing the adress of x
return 0;
这是正确的,实际上&p
是&x+4
,在32位体系结构上是正确的,因为它们是在堆栈上分配的。int x=5;
int x = 5;
int *p = &x; // b stores addres of variabile a
cout << "Adress of x is : " << p <<endl; // printing the adress of a
cout << "Value of x is : " << *p << endl; // printing the variabile stores in a
int **pp = &p; // pp stores thea adress of p
cout <<"Adress of p is" << pp << endl; //printing the adress of p
cout << "Adress of x is" <<*pp << endl; //printing the adress of x
return 0;
int*p=&x;//b存储变量a的地址
coutintx=5;
int*p=&x;//b存储变量a的地址
coutintx=5;
int*p=&x;//b存储变量a的地址
coutintx=5;
int*p=&x;//b存储变量a的地址
你能把用过的编译器标记添加到你的帖子中吗?你的例子在Ubuntu上的G++4.8.4上可以很好地使用-O3:我在打印p和打印p时得到两种不同的输出。但是打印出来的东西…和x本身的地址相同。
和&x
是相同的。&p
是不同的,因为本质不同可能是我的编译器,我用一个链接更新了我的帖子,链接到了我下载它的地方。你展示的程序只打印一个值,而不是两个,所以不清楚你在比较什么。显示你正在运行的确切代码,以及程序生成的输出,其中包括两个值。你刚刚发现我的问题是我比较了ad来自不同程序运行的衣服。你能将使用过的编译器标志添加到帖子中吗?你的例子在Ubuntu上的G++4.8.4和-O3上运行得很好:我在打印p和打印p时得到两种不同的输出。但是打印出来的东西…与x本身的地址相同。
p
和&x
是相同的。&p
I这是不同的,正如性质所示。可能是我的编译器,我更新了我的帖子,并添加了一个链接,指向我下载它的地方。你显示的程序只打印一个值,而不是两个值,因此不清楚你在与其他值进行比较。显示你正在运行的确切代码,以及程序生成的输出,其中包括两个值。你刚刚明白了我的问题是,我比较了不同程序运行时的地址。你能将使用过的编译器标志添加到你的帖子中吗?你的例子在Ubuntu上的G++4.8.4上使用-O3可以很好地工作:我在打印p和打印p时得到两种不同的输出。但是打印出来的…与x本身的地址相同。
p
和&x
都是一样的。&p
是不同的,因为本质不同。可能是我的编译器,我更新了我的帖子,并添加了一个链接,指向我下载它的地方。您显示的程序只打印一个值,而不是两个值,因此不清楚您与其他值的比较。显示您正在运行的确切代码,以及程序生成的输出,其中包括both values.ya刚刚发现我的问题是,我比较了不同程序运行的地址。你能将使用过的编译器标志添加到帖子中吗?你的例子在Ubuntu上的G++4.8.4和-O3上运行得很好:当我打印p和打印p时,我得到两种不同的输出。但是打印出来的是与x本身相同的地址。
p
和&x
是一样的。&p
是不同的,这是本质上的不同。可能是我的编译器,我更新了我的帖子,并添加了一个链接,指向我下载它的地方。你显示的程序只打印一个值,而不是两个值,因此不清楚你在与其他值进行比较。显示你正在运行的确切代码,并在pro中输出gram Products,其中包括两个值。你刚刚发现我的问题是我比较了不同程序运行的地址。你我的问题在于我比较了不同程序运行的地址。为了得到正确的比较,我应该像你一样在一个程序运行中打印地址。你我的问题在于我比较了程序不同运行时的地址。为了获得正确的比较,我应该像你一样在一个程序运行时打印地址。是的,我的问题在于我比较了程序不同运行时的地址。为了获得正确的比较,我应该像你一样在一个程序运行时打印地址。是的,我的问题谎言在于我比较了我程序不同运行的地址。为了得到正确的比较,我应该像你一样在一个程序运行中打印地址。