[英]How do I prevent my pointer for an array element from being reassigned
I'm pretty new to C so maybe I'm using pointers incorrectly.我对 C 很陌生,所以也许我使用的指针不正确。 I'm working on a larger program and where I'm running into an issue where a pointer, pointing at the first element of an array is constantly changing as the array changes (using a PriorityQueue, so it's constantly being rearranged and so I'm running into issues).
我正在开发一个更大的程序,我遇到了一个问题,即指向数组第一个元素的指针随着数组的变化而不断变化(使用 PriorityQueue,所以它不断被重新排列,所以我'我遇到了问题)。
I've made a very basic example of what's going on:我已经为正在发生的事情做了一个非常基本的例子:
#include <stdio.h>
int main() {
int testArray[] = {0, 1, 2, 3};
int *pFirstElement = &testArray[0];
printf("My first element: %d\n", *pFirstElement); // Prints 0
testArray[0] = 9;
printf("My first element: %d", *pFirstElement); // Prints 9 -> I want it to print 0
return 0;
}
I'd like a way so that pFirstElement does not get reassigned until I manually reassign pFirstElement to be the first element of the array.我想要一种方法,以便在我手动将 pFirstElement 重新分配为数组的第一个元素之前,不会重新分配 pFirstElement。 Basically, modifications to the array should not change the value of pFirstElement.
基本上,对数组的修改不应更改 pFirstElement 的值。 Is it because a pointer is pointing to the memory address of
testArray[0]
, and instead should I not be using a Pointer?是因为指针指向
testArray[0]
的 memory 地址,而我不应该使用指针吗?
The reason I used a pointer is that I can initialize it to NULL, and then a use case for my PriorityQueue is if the pointer is NULL, I know I need to dequeue basically.我使用指针的原因是我可以将它初始化为 NULL,然后我的 PriorityQueue 的一个用例是如果指针是 NULL,我知道我基本上需要出队。 Not sure how else I could do that logic.
不知道我还能怎么做这个逻辑。
Any advice is helpful, thanks!任何建议都有帮助,谢谢!
Since pFirstElement
is simply a reference to the memory in which first element of the array is stored, reassigning the first element of the array will change the value that pFirstElement
is pointing to.由于
pFirstElement
只是对存储数组第一个元素的 memory 的引用,重新分配数组的第一个元素将更改pFirstElement
指向的值。
Setting an int
variable instead of an int *
to testArray[0]
instead of &testArray[0]
will create a copy of the data instead of simply storing a new reference to it.将
int
变量而不是int *
设置为testArray[0]
而不是&testArray[0]
将创建数据的副本,而不是简单地存储对它的新引用。 The copy is now independent of the data in testArray
.该副本现在独立于
testArray
中的数据。
Creating a copy of the data is the only way.创建数据的副本是唯一的方法。 A pointer by itself cannot remember past values of the memory it points to.
指针本身不能记住它指向的 memory 的过去值。
The variable pFirstElement
points to the position in memory, where the first array element is.变量
pFirstElement
指向memory中的position,这里是第一个数组元素。 If you now access the value the pointer points to, you access the first element of the array.如果现在访问指针指向的值,则访问数组的第一个元素。 If you simply want a copy of first element use
int firstElement = testArray[0];
如果您只是想要第一个元素的副本,请使用
int firstElement = testArray[0];
, otherwise *pFirstElement =...
is equivalent to textArray[0] =...
. ,否则
*pFirstElement =...
等同于textArray[0] =...
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.