简体   繁体   English

如何防止我的数组元素指针被重新分配

[英]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.

相关问题 如何从C函数返回指向数组的指针? - How do I return a pointer to an array from my function in C? 如何获得用指针指向的二维数组的宽度/高度? - How do I get the width/height of a 2d array being pointed to with my pointer? 如何为结构体中的指针所指向的数组元素插入值 - How do I insert a value for an element of an array that is pointed by a pointer in a struct 如何将双指针设置回第一个数组元素? - How do I set a double pointer back to the first array element? 如何将单个字符串分配给一个指针数组中的元素? - how do I assign individual string to an element in one array of pointer? 如何防止设备弹出/安全移除? - How do I NOT prevent a device from being eject/safely removed? C 中函数指针的数组。如何使用枚举从数组中选择我想要的元素? - Array to function pointer in C. How do I select which element I want from the array using enums? 如何从类型“ void *”发送指向数组中元素的指针? - How can I send a pointer to element in array from type `void*`? 如何从指向该数组的指针中提取“ struct *”数组(在JNA中)? - How do I extract an array of `struct*` from a pointer to that array (in JNA)? 如何使用指向字符数组的指针一次访问一个数组元素? - How do I access one array element at a time using pointer to a character array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM