How to write a copy function for an array of size 2? using c
I want it so i can pass it to a generic code
this is the define:
/** Key element data type for map container */
typedef void *MapKeyElement;
this is the copy function that i wrote:
MapKeyElement gameCopyKey(MapKeyElement array_to_copy)
{
if(!array_to_copy)
{
return NULL ;
}
int** array=malloc(sizeof(**array));
if(!array)
{
return NULL;
}
int *tmp=(int*)array_to_copy;
*array[0]= tmp[0];
*array[1]= tmp[1];
return array;
}
but i don't know if it's the write way to do it or not ? does anyone have an idea?
int** array=malloc(sizeof(**array));looks wrong. It should beint** array=malloc(sizeof(*array) * 2);to allocatearray[0]andarray[1]. Also don't forget to initialize the elements to some valid address before dereferencing them.