Solved

memcpy

Posted on 2006-11-02
5
1,004 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

691 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