Say I have an existing array int *array
int *new_array = NULL;
new_array = array;
int *copy(int *array)
{
return array;
}
int* new_array = NULL;
new_array = copy(array);
What's the difference between the two?
Thanks for the help!
Arrays cannot be assigned in C language. In your examples, you assign a pointer with reference to the first element of the array. Your second snippet also only assigns pointer with pointer returned by the function. It does not copy data referenced by those pointers.
Say I have an existing array int *array
It is not an array - it is a pointer to int
If you want to copy elements of one array to another array you need to use function memcpy or memmove
int array1[5] = {1,2,3,4,5}
int array2[5];
memcpy(array2, array1, sizeof(array1));
arrayandnew_arraypoints to the same address, meaning, the same data.memcpyif you want to actually copy the data fromarraytonew_array.int *foo(void) { int array[something]; return array; }thanint *copy(int *array) { return array; }?