Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use memcpy in 3d arrays in C

Tags:

c

I have a 3 dimensional array int32_t x[1024][4][256]. I need to copy all the elements of the array to another array of same type and size int32_t y[1024][4][256].

Can i use memcpy(y, x, sizeof(x));?

after that can I access the elements of array y same as that of x?

like image 815
user1390048 Avatar asked May 14 '12 22:05

user1390048


1 Answers

can i use memcpy(y, x, sizeof(x))?

Yes.

after that can I access the elements of array y same as that of x?

Yes.

Note that this approach breaks down if you allocate the array dynamically (e.g. using malloc()). If you do that, sizeof() will no longer give you the size of the array (it will give the size of the pointer), and you'll have to keep track of the array dimensions yourself.

like image 117
NPE Avatar answered Oct 12 '22 23:10

NPE