Solved

memcpy

Posted on 2006-11-02
5
969 Views
Last Modified: 2008-01-09
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
Comment
Question by:IAJWDDIY
  • 3
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17862496
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
 
LVL 8

Expert Comment

by:Exceter
ID: 17862510
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
 
LVL 8

Expert Comment

by:Exceter
ID: 17862519
P.S. nm, age1, and age2 could also contain binary data but given the context I find this to be unlikely.
0
 
LVL 8

Accepted Solution

by:
Exceter earned 500 total points
ID: 17862543
>> 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
 

Author Comment

by:IAJWDDIY
ID: 17862837
Thank you.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now