Reputation: 235
I have a function within a file that takes in a pointer to an array represented by a double pointer. I want to copy the contents of a global array within the file to it. But when I dump the array, the data is not correct. What am I doing wrong?
Note: I have malloc'd enough memory for the double pointer to array and pass it into the function using its address
File1:
struct Data DataArray[10]; //global structure
CopyGlobalArray (void ***ArrayBuffer)
{
memcpy(*ArrayBuffer, &DataArray[0], sizeof(DataArray);
}
File2:
function() {
Data **MyArray = malloc(sizeof(Data) * 10);
CopyGlobalArray(&MyArray);
for (index =0; index<10 ; index++){
printf(MyArray[Index]->FirstName);
printf(MyArray[Index]->LastName);
}
}
Upvotes: 0
Views: 130
Reputation: 6298
In you declare MyArray
as a doble pointer
Data **MyArray = malloc(sizeof(Data) * 10);
the result of malloc
should be casted to the double pointer:
struct Data **MyArray = (struct Data**) malloc(sizeof(Data) * 10);
But providing a single pointer to the array of struct Data
should be sufficient for your needs:
void CopyGlobalArray (void *ArrayBuffer)
{
memcpy(ArrayBuffer, DataArray, sizeof(DataArray));
}
This is how could you use it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Data
{
char * FirstName;
char * LastName;
};
// global structure
struct Data DataArray[] = { {.FirstName = "Fred", .LastName= "Smith"}, {.FirstName = "Eva", .LastName= "White"}, {.FirstName = "John", .LastName= "Rock"} };
void CopyGlobalArray (void *ArrayBuffer)
{
memcpy(ArrayBuffer, DataArray, sizeof(DataArray));
}
void print(struct Data *array )
{
for(size_t i=0; i < sizeof(DataArray)/sizeof(DataArray[0]); i++)
{
printf("%s", array[i].FirstName);
printf("%s\n", array[i].LastName);
}
}
struct Data *function()
{
struct Data *MyArray = (struct Data *) (malloc (sizeof(DataArray) ) );
CopyGlobalArray (MyArray);
// Copied array
printf("\nMyArray:\n");
print(MyArray);
return MyArray;
}
int main(void)
{
// Original
printf("\nDataArray:\n");
print(DataArray);
struct Data *MyArray1 = function();
printf("\nMyArray1:\n");
print(MyArray1);
free(MyArray1); // free the array allocated inside function()
return 0;
}
Output:
DataArray:
FredSmith
EvaWhite
JohnRock
MyArray:
FredSmith
EvaWhite
JohnRock
MyArray1:
FredSmith
EvaWhite
JohnRock
Upvotes: 0
Reputation: 474
Why are you passing double pointer to void*.You can just do this by a single void*. You can do like this:
File1:
struct Data DataArray[10]; //global structure
CopyGlobalArray (void *ArrayBuffer)
{
memcpy(ArrayBuffer, DataArray, sizeof(DataArray);
}
File2:
function() {
Data *MyArray = malloc(sizeof(Data) * 10);
CopyGlobalArray(MyArray);
}
Upvotes: 0
Reputation: 25286
As long as the copy function isn't charged with alocating the memory for the copy, a single pointer is sufficient:
struct Data DataArray[10]; //global structure
CopyGlobalArray (void *ArrayBuffer)
{
memcpy(ArrayBuffer, DataArray, sizeof(DataArray));
}
If the function is charged with allocating the memory for the copy, you need a double pointer or use the return value, the latter being prefered:
struct Data DataArray[10]; //global structure
void *CopyGlobalArray (void)
{
void *copy;
if (copy=malloc(sizeof(DataArray)))
memcpy(copy, DataArray, sizeof(DataArray));
return copy;
}
Upvotes: 0
Reputation: 409364
It might be hard to visualize, so lets draw the relationship:
The malloc
function returns a pointer:
+-------------------+ +----------------------+ | pointer to memory | --> | The actual memory... | +-------------------+ +----------------------+
However, you declare MyArray
as a pointer to a pointer (do struct Data
), which is more like
+---------+ +-------------------+ +----------------------+ | MyArray | --> | pointer to memory | --> | The actual memory... | +---------+ +-------------------+ +----------------------+
The problem is that MyArray
doesn't really point to a pointer, it points directly to the actual memory, which means the code will not be correct.
Upvotes: 1