• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1029
  • Last Modified:

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);
                .......
               
}
0
IAJWDDIY
Asked:
IAJWDDIY
  • 3
1 Solution
 
ozoCommented:
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
0
 
ExceterCommented:
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
0
 
ExceterCommented:
P.S. nm, age1, and age2 could also contain binary data but given the context I find this to be unlikely.
0
 
ExceterCommented:
>> Can anyone please explain in simple terms what the following function does, so I can try to find a VB.Net equivalent ?
Did you mean set_name or memcpy? If you meant memcpy, you only need to read the following from my previous posts,

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.

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

Cheers!
Exceter
0
 
IAJWDDIYAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now