|
|
I have a schoolproject in c++ where i need to
use a "person" struct. I instantiate 3 struct
members with the following line:
person pp1, pp2, pp3;
person *pp = new person; //somekind of allocation i tried
//in order to refer to the
//copyparam function in another cppfile
//The definition for the copyparam function.
person& copyparam(person&, person&)
{
strncpy(pp1, pp2, 20);
}
------------------------------------------
I need to use the strncpy function to copy
the contents of pp2 to pp1 and return a reference
for pp2. I really don't know how I should approach
this problem. And I hope that someone could help
me. i believe the line
strncpy(pp1, pp2, 20) is wrong but i don't really
know how i should refer to the parameters that
copyparam takes.
calls from the main program:
person
copyparam(pp1, pp2);
copyparam(pp1, pp2) = pp3;
please help me!
thank you.
|
|
|
|
some code to refer :-
// learn strncpy
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strncpy(string, str1, 3);
string[3] = '\0';
printf("%s\n", string);
return 0;
}
// learn struct
#include <string.h>
struct my_struct {
char name[80], phone_number[80];
int age, height;
} my_friend;
void func() {
strcpy(my_friend.name,"Mr. Wizard"); /* accessing an element */
}
struct my_struct my_friends[100]; /* declaring additional variables */
|
|
|
|
|
|
|
|
|
// |