Reputation: 41
I have a char array with the size of 128. I want to copy the first 32 to another char array. This is how i'm trying to do it:
char ticket[128];
data.readBytes(ticketLength, (BYTE*)ticket);
char sessionID[32];
strcpy(sessionID, ticket);
int userID = bdAuthService::checkLogin(sessionID);
The contents of "ticket" are "11aa14462ac96a9b389686672b99fa9e1IvtooKO6eVxVHO6URIQld8jFaceTaker"
and when i'm trying to pass the sessionID to the checkLogin function it gets the same contents as ticket "11aa14462ac96a9b389686672b99fa9e1IvtooKO6eVxVHO6URIQld8jFaceTaker"
.
Can anyone help me here?
Upvotes: 0
Views: 6548
Reputation: 310930
Use memcpy
function instead.
memcpy( sessionID, ticket, sizeof( sessionID ) );
Take into account that sessionID won;t contain a string. It will contain "raw" characters.
As for function strcpy
then it is designed to copy strings that is a sequence of characters termintaed by zero.
So in case of your code fragment it will copy as many characters as there are characters in ticket
before the character with value '\0' provided that such a character is present in ticket
.
Upvotes: 4
Reputation: 4770
You can use strncpy which allows you to specify the number of bytes to copy.
Upvotes: 0