gpt4 book ai didi

c - 将结构数组分配给指针

转载 作者:行者123 更新时间:2023-12-02 07:48:56 24 4
gpt4 key购买 nike

我已经初始化了一个结构数组(仅包含一个名为 name 的 char 字符串。然后将这个结构数组分配给一个指针,如下所示:

location locations[2] = {{"Padstow", 50.5384, -4.9378}, 
{"Newquay", 50.412, -5.0757}};

int location_size = 2;

location *loc_ptr;
loc_ptr = &locations[0];

// pick an element to remove
location element = loc_ptr[1];

remove_element(loc_ptr, location_size, element);

然后我将这个指针传递给一个函数。在这个函数中,我正在删除数组的一个元素。这是通过遍历当前数组并创建一个新数组来完成的。新数组包含我期望的数据。

remove_element 函数中:

void remove_element(location *ptr, int count, location element) {

// create the new array
location new_locations[count-1];

// go through and pick out the non-matching element

// create the new pointer
location *new_ptr;
new_ptr = &new_locations[0];

// assign the new array to the original pointer
ptr = new_ptr;
}

但是,它并没有改变原始数据。你能解释一下我在将这个新数组分配给我的原始指针时做错了什么吗?

最佳答案

我已经有一段时间没有用 C 语言做过任何事情了,所以我有点生疏,但我会试一试...

我会看到(至少)有两种方法可以让它工作:要么从函数返回新数组的新指针并将其存储在旧指针中,类似于(这在语法上可能不正确):


loc_ptr = someFunctionCreatingTheNewArrayAndReturningAPointerToIt(loc_ptr);

另一种可能性是通过指针而不是值将 loc_ptr 传递给函数。基本上,您会将“指针到指针”作为参数传递给函数,指针到指针指向您的 loc_ptr。在函数内部,您从指针到指针取消引用数组内存地址以访问原始数组。创建并填充新数组后,将新数组的内存地址放入指针传递的参数中。

编辑:我举了一个关于这两种方式的简单示例,这实际上是在 C++ 中,但我 99% 确定,指针在 C 中的工作方式相同(抱歉,它有点冗长)。请注意,数组不会在任何地方释放,因此这会导致内存泄漏(但您应该了解按值传递还是按指针传递):

#include <iostream>
#include <string.h>

struct location
{
std::string name;
double lat;
double lon;
};

location* createNewArrayAndReturnPointer(location* loc)
{
std::cout << "-- Replacing array, name of the first location in old array is " + loc->name << std::endl;
location* newLoc = new location[2]; //Local pointer-variable, creating new array and storing array address to it
newLoc[0].name = "Replacing pointer by return value";

return newLoc; //Return new pointer
}

void modifyViaGivenPointerToPointer(location** loc_ptr_to_ptr)
{
location* loc = *loc_ptr_to_ptr; //De-referencing the array address from the pointer-to-pointer, storing to local pointer-variable
std::cout << "-- Modifying pointer, name of the first location pointed originally is " + loc->name << std::endl;

location* newLoc = new location[2]; //Creating new array and storing to local pointer-variable
newLoc[0].name = "From modifyViaGivenPointerToPointer";
*loc_ptr_to_ptr = newLoc; //Replacing the contents of given pointer-variable via dereference

}

void printNameOfFirstLocationInArray(location* loc_ptr)
{
std::cout << "The name of the first location pointer by loc_ptr is now " << loc_ptr->name << std::endl;
}

int main(void)
{
location locations[2] = {{"Padstow", 50.5384, -4.9378},
{"Newquay", 50.412, -5.0757}};

location* loc_ptr;
loc_ptr = &locations[0];

printNameOfFirstLocationInArray(loc_ptr);

//Returns new pointer from function and store it in the pointer-variable
loc_ptr = createNewArrayAndReturnPointer(loc_ptr);
printNameOfFirstLocationInArray(loc_ptr);

//Modifies the passed pointer-to-pointer, so it points to the new array after returning
modifyViaGivenPointerToPointer(&loc_ptr);
printNameOfFirstLocationInArray(loc_ptr);

return 0;
}

输出是:

The name of the first location pointer by loc_ptr is now Padstow
-- Replacing array, name of the first location in old array is Padstow
The name of the first location pointer by loc_ptr is now Replacing pointer by return value
-- Modifying pointer, name of the first location pointed originally is Replacing pointer by return value
The name of the first location pointer by loc_ptr is now From modifyViaGivenPointerToPointer

关于c - 将结构数组分配给指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4842919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com