C++指针 - 冲突声明和指针到指针
当我做出以下声明时:
int b;
int c;
int *b;
int *c;
编译时得到以下输出:
注意:I'我使用 Cygwin 控制台,这就是为什么我无法复制粘贴输出
那么,我们是否可以得出这样的结论:当我们声明一个指针
变量时,它同时 一个可以自己保存数据的普通变量?换句话说,一个具有地址和值的内存位置?
我问这个是因为我想尝试指针到指针
?
例如,如果我有“int **c”,我怎样才能让它保存以下内容:
(b)的值/(b)的地址/(a)的值/(a)的地址
还有,有int ***c
吗?
多谢。
When I made the following declarations:
int b;
int c;
int *b;
int *c;
I got the following output when compiled:
Note: I'm using Cygwin console, that is why I couldn't copy-paste output
So, do we conclude here that when we declare a pointer
variable, it is at the same time an ordinary varibale that can hold data on its own? In other words, a memory location that has an address and a value?
I'm asking this since I want to try pointer-to-pointer
?
If I have `int **c' for example, how can I make it hold the following:
Value of (b) /Address of (b)/Value of (a)/ Address of (a)
And, is there int ***c
?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您在同一范围内声明两个具有相同名称的变量。这是不允许的。
You're declaring two variables with the same name in the same scope. This is not allowed.
显然,这解释了您看到的错误。
如何声明两个具有相同名称的变量?两个同名变量会导致冲突!
对此也有相同的解释:
两个变量具有相同的名称,因此存在冲突!
Obviously, this explains the error that you see.
How can you declare two variables with same name? Two variables with same name cause the conflict!
Same explanation for this as well:
Two variables with same name, hence the conflict!
指针是存储内存地址的变量。所以,是的,它有一个值和一个内存地址。
int *p = 0; int **pp = &p
.这是有效的,p
是一个存储在堆栈中的指针,其值为0和一些内存地址;pp
也在堆栈上分配,有另一个内存地址并保存p
的内存地址。内存地址必须存储在某处并占用固定的存储量。例如,根据您的实现,内存地址可以存储在四个字节中。在这种情况下,您有
sizeof(char)
= 1sizeof(char *)
= 4sizeof(
任何其他指针类型,包括指向指针的指针)
= 4。因此,通过执行
reinterpret_cast
,您的指针可以存储最多占用四个字节的任何类型。但你为什么要这样做呢?A pointer is a variable that stores a memory address. So, yes, it has a value, and a memory address.
int *p = 0; int **pp = &p
. This is valid,p
is a pointer that is stored in the stack and has a value of 0 and some memory address;pp
is also allocated on the stack, has another memory addres and holds the memory address ofp
.A memory address must be stored somewhere and takes a fixed amount of storage. For example a memory address could, depending on your implementation, be stored on four bytes. In this case you have
sizeof(char)
= 1sizeof(char *)
= 4sizeof(
any other pointer type, including pointer to pointer)
= 4.so, by performing
reinterpret_cast
s your pointer can store any type that takes up to four bytes. But why would you like to do so?是的,指针是一个具有值并占用一些内存的常规变量。不过,您收到错误的原因与此无关,这只是因为您尝试使用相同的名称声明两个不同的事物。
当然,你可以有指向指针的指针,甚至可以有指向指针的指针,甚至可以有更多级别的这种疯狂。唯一的问题是如何使用它们。我可以想象指向指针的指针有很多用途。对于更多级别,我只能想象“数组的数组”的用途,但它仍然是一些东西。
但是您不应该做的是将错误类型的值存储在变量中。如果你有一个指向指针的指针,你应该在那里存储某个指针(正确类型)的地址,没有别的,例如:
在这个例子中,将 a 的值存储在 c 中是绝对错误的,尽管它是可能的在 int 和指针具有相同大小的平台上。但这就像将文本字符串存储在 - 绝对毫无意义且危险。
Yes, a pointer is a regular variable that has value and occupies some memory. The reason for the error you get has nothing to do with this, though, it's simply because you try to declare two different things with the same name.
And of course you can have pointers to pointers or even pointers to pointers to pointers or even more levels of this insanity. The only question would be is how to use them. I can imagine a lot of uses for pointers to pointers. For more levels I can only imagine an "array of arrays" kind of use, but it's still something.
But what you shouldn't do is to store values of wrong type in a variable. If you have a pointer to pointer, you should store an address of some pointer (of correct type) there, nothing else, for example:
In this example, it would be absolutely wrong to store the value of a in c, although it's possible on platforms where int and pointers have the same size. But it would be like storing a text string in a - absolutely pointless and dangerous.
让我们教您一些基础知识。
b 是 int 类型的变量,保存整数值,例如 3、-28、49382
b 是 int* 类型的变量,保存指针。它可以是 NULL 或指向包含 int 的变量。因为它不是 const int*,所以您可以对其进行写入和读取。您可以移动它以指向不同的 int 变量,只要它们没有 const (或 volatile)限定符即可。您还可以使用指针指向某些动态分配的内存或此类内存数组的开头(例如 int *b = new int; 或 int *b = new int[N ])或静态数组中的某个位置。
所以 b 要么是整型变量,要么是指针变量。不可能两者兼而有之。
要回答你的最后一点,是的,你可以拥有指向指针的指针,并且深度没有限制,尽管使用
int ********p 显然会变得不可读。
Let's teach you some basics.
b is a variable of type int, that holds an integral value, such as 3, -28, 49382
b is a variable of type int* that holds a pointer. It can be NULL or point to a variable that contains an int. Because it is not const int* you can write to it as well as read from it. You can move it around to point to different int variables as long as they have no const (or volatile) qualifiers. You can also use the pointer to point to some dynamically-allocated memory or the start of an array of such (eg
int *b = new int;
orint *b = new int[N]
) or to a location in a static array.So b is either an integer variable, or a pointer variable. It can't be both.
To answer your last point, yes you can have pointers to pointers, and there is no limit in the depth although it would obviously become unreadable to have
int *******p
是的,这是完全正确的。指针是保存地址的变量。在您的代码中,您声明了两个具有相同名称的变量(在同一命名空间中)。这是不允许的。
Yes that's exactly correct. A pointer is a variable that holds an address. In your code, you have declared two variables with the same name (in the same namespace). That it not allowed.