Link to home
Start Free TrialLog in
Avatar of IAJWDDIY
IAJWDDIY

asked on

memcpy

Hello,

Can anyone please explain in simple terms what the following function does, so I can try to find a VB.Net equivalent ?

Thank you.

void set_name(unsigned char *nm, struct prs *prs)  {
                 memcpy(prs->age1,nm+8,8);
                 memcpy(prs->age2,nm,8);
                .......
               
}
Avatar of ozo
ozo
Flag of United States of America image

it copies the first 8 characters of nm into the the place where the age2 field of the prs structure points
and the next 8 characters of nm into the the place where the age1 field of the prs structure points
To begin, you need to post the entire function for a diffinitive answer.

memcpy copys the contents of an array to another array. It takes 3 arguments. The first is a pointer to the destination array. The second is a pointer to the source array. The third is a numeric value signifying the number of bytes to copy.

The code you posted is the beginning of a function named set_name which takes two arguments. The first is a pointer to an array of type unsigned char or C-string as they are often called because strings in C are implemented as arrays. The second argument is a pointer to a data structure. This makes interpreting the code harder as I don't know what data members are in the structure.

>> memcpy(prs->age1,nm+8,8)
This copies the array age1, which I will assume is a string 7 bytes in length (the 8th would be needed for the terminating null byes that tells string functions they have reached the end of the string), and copies it to the unsigned char array nm starting at the 9th position in the array, 8 is the 9th position as 0 is the first position.

>> memcpy(prs->age2,nm,8);
Performs essentially the same action as the previous line except that this time it copies the contents of the array age2 to the beginning of the unsigned char array nm (position 0).

I'm sorry if that's a little cryptic but you'll need to post more code for me to tell you any more. I would recommend posting the complete code of the function, the declaration of struct prs, and what the program is supposed to do.

The prs declaration probably looks something like this,
struct prs
{
char age1[9];
char age2[9];

....
};

More on memcpy here,
http://www.cplusplus.com/ref/cstring/memcpy.html

Cheers!
Exceter
P.S. nm, age1, and age2 could also contain binary data but given the context I find this to be unlikely.
ASKER CERTIFIED SOLUTION
Avatar of Exceter
Exceter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IAJWDDIY
IAJWDDIY

ASKER

Thank you.