Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my VC++ code,

I want to move value to another variables like as:

char* buf ="12345";

char logbuffer[1024];

strcpy(logbuffer, buf);

struct xx{
int a;
char b[1024];
}

xx yy;
memcpy(&yy.b, logbuffer, strlen(logbuffer));

I guess this is right.

but

if the logbuffer and yy are array like this;

char logbuffer[2][1024];

xx yy[2];

and if I try to move the value of buf to those variables like this:

strcpy(logbuffer[1], buf) and, or

memcpy(&yy[1].b, logbuffer[1], strlen(logbuffer[1]));

make compile error to say ambiguous sentense....

I don't what is main problem of this.

Please let me know the answer and correct method.

Thank you in advance.

What I have tried:

1 more days wasted of this problem.
Posted
Updated 10-Jun-17 5:38am
Comments
Michael Haephrati 10-Jun-17 10:35am    
The question is so unclear in terms of its English that I had to read it 5 times and yet I don't understand it at all. In my answer I gave a generic explanation but I can't comment on your source code itself because it isn't clear, where does it start and where does it end. What didn't work and what did?

Don't use memcpy.
To copy one string to another, use strcpy().
strcpy received a pointer to each array of characters, or to be specific, to the first character in a NULL terminated array.
 
Share this answer
 
Try this:
C++
char* buf = "12345";
char logbuffer[1024];

struct xx {
    int a;
    char b[1024];
};
xx yy;

strcpy(logbuffer, buf);
memcpy(yy.b, logbuffer, strlen(logbuffer)); // do not use & on yy

char logbufferb[2][1024];
xx yyb[2];

strcpy(&logbufferb[1][0], buf);  // do use & on logbufferb[1]

memcpy(yyb[1].b, &logbufferb[1][0], strlen(&logbufferb[1][0])); // see above comments

Note the above comments with respect to the addressof operator (&). Also note that using memcpy does not null terminate the strings, so you may have problems with it, always use strcpy.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900