I haven't done anything in C for quite a while, so I'm a bit rusty, but I'll give it a try...
I'd see there are (at least) two ways to get this working: either return the new pointer for the new array from the function and store it in the old pointer, something like (this might be syntactically incorrect):
loc_ptr = someFunctionCreatingTheNewArrayAndReturningAPointerToIt(loc_ptr);
Another possibility is to pass the loc_ptr to the function by-pointer instead of by-value. Basically, you'll pass a "pointer-to-pointer" as parameter into the function, the pointer-to-pointer pointing to you loc_ptr. Inside the function, you dereference the arrays memoryaddress from the pointer-to-pointer to access the original array. Once you've created and filled the new array, put the memory address of the new array into the parameter passed by-pointer.
Edit: I whipped up a quick example of both ways, this is actually in C++, but I'm 99% sure, that the pointers work the same in C (sorry if it's a bit verbose). Note that the arrays aren't freed anywhere, so this would cause memory leaks (but you should get the idea of passing by-value vs. by-pointer):
#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 output is:
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
// modify the array. Also, you mentionptronly once and you didn't explain where it came from. You hadloc_ptrat first.