Reputation: 105992
To copy an array b[]
into the array a[]
, one can use function memcpy
as follows;
memcpy(a,b,sizeof(a))
.
But memcpy
simply copies bytes from one place to another.
My questions are:
1.How
memcpy
copies elements of arrayb[]
intoa[]
by copying bytes?
2.Whysizeof(a)
is supplied as arguments?
I am new to programming so, be gentle.
Upvotes: 0
Views: 3808
Reputation: 652
#include <string.h>
void *
memcpy(void *s1, const void *s2, register size_t n)
{
register char *p1 = s1;
register const char *p2 = s2;
if(n) {
n++;
while (--n > 0) {
*p1++ = *p2++;
}
}
return s1;
}
Here is a source code for memcpy. As you can see it literally moves through each element from register 1 and moves it into register 2 one by one.
size of is used so it knows how big the register is so basically how many elements it must skip through before it has copied the whole register.
Upvotes: 2
Reputation: 3845
First one is an interesting question. Yes, it is a simple binary copy, but an order of copying matters. Say, if you have an overlaping areas, and &a[0] > &b[0], what would happen? If it is from left to right, memcpy would simply overwrite data from b[] while copying.
AFAIK, the particular order of copying isn't specified by any standart and it is recomended to use memmove instead as a safer alternative, but still.
Upvotes: 0
Reputation: 22905
sizeof(a)
is the total size of the array a
. For example, in
int a[3];
printf("%d", sizeof(a));
sizeof a
will be 12
on most systems (since int
is usually 4 bytes and you have 3 of them).
memcpy
doesn't know that a
is an array. All it knows is that a
points to some memory address, so you have to tell it how many bytes to pass. Most places describe memcpy's signature as:
void *memcpy(void *dst, const void *src, size_t nbytes)
Upvotes: 5
Reputation: 4391
Sizeof(a) is supplied as third arguments because it is the size in bytes of the array a. In fact sizeof called with an array as argument returns the total number of bytes in that array instead of the size of the pointer represented by the array identifier.
Upvotes: 0
Reputation: 1922
memcpy
works just like how assembly instruction MOV
works (obviously, memcpy
must be implemented using MOV
). Difference is just that, we move a block of data, instead of one variable. Size of the block is required since the process need to know how much bytes is to be copied from one location to another.
Upvotes: 1
Reputation: 24867
1) In C, all arrays are really just bytes of data in a long sequence, one after another. There is no interspersed meta data or anything like that, so copying the bytes of an array, means copying the array itself with no loss of data, regardless of what type the array is made up of. Only the total size matters.
2) This is as Kerrek SB said, "because you cannot have function arguments of array type in C". That ties in with 1 - since there is no meta data in C arrays, the function gets no information on how large an array is, just where in memory it begins. The size is so it can know when to stop write data.
Upvotes: 0