Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.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++ 如何在c/c++;?_C++_C - Fatal编程技术网

C++ 如何在c/c++;?

C++ 如何在c/c++;?,c++,c,C++,C,如何在c/c++中获取指针的地址 我有下面的代码 int a =10; int *p = &a; 那么如何获取指针p的地址呢? 现在我想打印p的地址,我该怎么办 打印(“%s”,“??”)我传递给???的内容。&a给出a的地址-&p给出p的地址 int * * p_to_p = &p; 要获取p的地址,请执行以下操作: int **pp = &p; 你可以继续: int ***ppp = &pp; int ****pppp = &ppp; ...

如何在c/c++中获取指针的地址

我有下面的代码

int a =10;
int *p = &a;
那么如何获取指针
p
的地址呢? 现在我想打印p的地址,我该怎么办


打印(“%s”,“??”)我传递给???的内容。

&a
给出
a的地址-
&p
给出
p的地址

int * * p_to_p = &p;

要获取p的地址,请执行以下操作:

int **pp = &p;
你可以继续:

int ***ppp = &pp;
int ****pppp = &ppp;
...
或者,仅在C++11中,您可以执行以下操作:

auto pp = std::addressof(p);
cout << "addr: " << pp;
// Declaration and assign variable a
int a = 7;
// Declaration pointer b
int* b;
// Assign address of variable a to pointer b
b = &a;

// Declaration pointer c
int** c;
// Assign address of pointer b to pointer c
c = &b;

std::cout << "a: " << a << "\n";       // Print value of variable a
std::cout << "&a: " << &a << "\n";     // Print address of variable a

std::cout << "" << "" << "\n";

std::cout << "b: " << b << "\n";       // Print address of variable a
std::cout << "*b: " << *b << "\n";     // Print value of variable a
std::cout << "&b: " << &b << "\n";     // Print address of pointer b

std::cout << "" << "" << "\n";

std::cout << "c: " << c << "\n";       // Print address of pointer b
std::cout << "**c: " << **c << "\n";   // Print value of variable a
std::cout << "*c: " << *c << "\n";     // Print address of variable a
std::cout << "&c: " << &c << "\n";     // Print address of pointer c
要以C语言打印地址,大多数编译器支持
%p
,因此您只需执行以下操作:

printf("addr: %p", pp);
否则,您需要强制转换它(假设为32位平台)

在C++中你可以做:

auto pp = std::addressof(p);
cout << "addr: " << pp;
// Declaration and assign variable a
int a = 7;
// Declaration pointer b
int* b;
// Assign address of variable a to pointer b
b = &a;

// Declaration pointer c
int** c;
// Assign address of pointer b to pointer c
c = &b;

std::cout << "a: " << a << "\n";       // Print value of variable a
std::cout << "&a: " << &a << "\n";     // Print address of variable a

std::cout << "" << "" << "\n";

std::cout << "b: " << b << "\n";       // Print address of variable a
std::cout << "*b: " << *b << "\n";     // Print value of variable a
std::cout << "&b: " << &b << "\n";     // Print address of pointer b

std::cout << "" << "" << "\n";

std::cout << "c: " << c << "\n";       // Print address of pointer b
std::cout << "**c: " << **c << "\n";   // Print value of variable a
std::cout << "*c: " << *c << "\n";     // Print address of variable a
std::cout << "&c: " << &c << "\n";     // Print address of pointer c
cout
要获取a的地址,您需要:
&a
(a的地址
),它返回一个
int*
(指向int的指针)

然后将a的地址存储在
p
中,其类型为
int*

最后,如果您执行
&p
操作,您将获得
p
的地址,该地址的类型为
int**
,即指向int的指针:

int** p_ptr = &p;
刚刚看到您的编辑:

要打印指针的地址,您需要将其转换为:

printf("address of pointer is: 0x%0X\n", (unsigned)&p);
printf("address of pointer to pointer is: 0x%0X\n", (unsigned)&p_ptr);
或者,如果您的printf支持,请使用
%p

printf("address of pointer is: %p\n", p);
printf("address of pointer to pointer is: %p\n", p_ptr);
您可以在C中使用%p

在C中:

在C++中:

cout<<"Address of pointer p is: "<<p
cout你可以用这个

在C中

在C中++

cout<<p;
cout具有此C源代码:

int a = 10;
int * ptr = &a;
用这个

printf("The address of ptr is %p\n", (void *) &ptr);
打印
ptr
的地址

请注意,转换说明符
p
是唯一打印指针值的转换说明符它定义为仅与
void*
类型指针一起使用

p

void*指针参数以十六进制打印(就像 通过%#x或%#lx


您可以使用
%p
格式化程序。在打印之前,最好将指针强制转换为void*

C标准规定:

该参数应是指向void的指针。指针的值以实现定义的方式转换为一系列打印字符

以下是您的操作方法:

printf("%p", (void*)p);

C++中,您可以执行以下操作:

auto pp = std::addressof(p);
cout << "addr: " << pp;
// Declaration and assign variable a
int a = 7;
// Declaration pointer b
int* b;
// Assign address of variable a to pointer b
b = &a;

// Declaration pointer c
int** c;
// Assign address of pointer b to pointer c
c = &b;

std::cout << "a: " << a << "\n";       // Print value of variable a
std::cout << "&a: " << &a << "\n";     // Print address of variable a

std::cout << "" << "" << "\n";

std::cout << "b: " << b << "\n";       // Print address of variable a
std::cout << "*b: " << *b << "\n";     // Print value of variable a
std::cout << "&b: " << &b << "\n";     // Print address of pointer b

std::cout << "" << "" << "\n";

std::cout << "c: " << c << "\n";       // Print address of pointer b
std::cout << "**c: " << **c << "\n";   // Print value of variable a
std::cout << "*c: " << *c << "\n";     // Print address of variable a
std::cout << "&c: " << &c << "\n";     // Print address of pointer c
//声明并分配变量a
INTA=7;
//声明指针b
int*b;
//将变量a的地址分配给指针b
b=&a;
//声明指针c
int**c;
//将指针b的地址分配给指针c
c=&b;

首先,您应该理解指针并不复杂。指针显示变量的地址

例如:

// this might works fine since the out put is an integer as its expected.
printf("%d\n", *p); 

// but to get the address:
printf("%p\n", p); 
inta=10;
int*p=&a;//这意味着将变量“a”的指针指定给int指针变量“p”
而且,您应该理解“指针是地址”和“地址是数值”。因此,可以将变量的地址作为整数

inta=10;
无符号长地址=(无符号长地址)&a;
//比较
printf(“%p\n”、&a);
printf(“%ld\n”,地址);
产量低于

0x7fff1216619c
7fff1216619c
注:

如果您使用64位计算机,则无法通过下面的方式获取指针

inta=10;
无符号整数地址=(无符号整数)&a;
因为在64位机器上指针是8字节(64位),而int是4字节。所以,不能给4字节变量指定8字节的内存地址

必须使用
long-long
long
获取变量的地址

  • long-long
    始终为8字节
  • 为32位机器编译代码时,
    long
    为4字节
  • 为64位机器编译代码时,
    long
    为8字节

因此,您应该使用
long
来接收指针。

如果您试图从Linux终端编译这些代码,可能会出现以下错误

参数类型应为int

这是因为,当您试图通过
printf
获取内存地址时,无法将其指定为视频中显示的
%d
。 不要那样,试着把
%p
放进去

例如:

// this might works fine since the out put is an integer as its expected.
printf("%d\n", *p); 

// but to get the address:
printf("%p\n", p); 

不应使用“%s”打印类似整数的值。在上面的示例中,
coutI尝试了cout,如何将指针地址还原为var?例如
int*pp=getponterexample(address)
?这似乎是错误的,p前面缺少一个(&M)