I am trying copy an array (temp)
from another array a
.
But I have it is not happening.
Fig-1
int main()
{
typedef int arr_1[3];
arr_1 arr[4];
arr_1 *temp;
arr_1 a[3] = {1, 2, 3};
memset(&temp, 0, sizeof(temp));
memcpy(temp, a, sizeof(temp));
}
But when I tried with a simple program like below,
Fig-2
main()
{
int abc[3], def[3];
def[3] = {1, 2, 3};
memcpy(abc, def, sizeof(abc));
}
This above code (fig-2)
worked really fine for me.
But fig-1
is not working for me. Both are alomost same.
But why the fig-1
is not working??
In the C Programming Language, the memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. It returns a pointer to the destination. The memcpy function may not work if the objects overlap.
The memcpy function is used to copy a block of data from a source address to a destination address.
Because temp
is not an array, it's a pointer and therefore sizeof(temp)
has absolutely no relation to the array.
You want to change the memcpy
to use sizeof(a)
. You will also want to give temp
a sane value before copying to it, otherwise the program has undefined behavior.
You must allocate memory for temp
with malloc()
for example. For now it`s just an uninitialized pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With