I haven't long time in touch with C language. I have some questions related to chinese words and strncpy.
char* testString = "你好嗎?"
sizeof(testString) => it prints out 4.
strlen(testString) => it prints out 10.
When i want to copy to another char array, i have some issue.
char msgArray[7]; /* This is just an example. Due to some limitation, we have limited the buffer size. */
If i want to copy the data, i need to check
if (sizeof(testString) < sizeof(msgArray)) {
strncopy(msgArray, testString, sizeof(msgArray));
}
It will have problem. The result is it will only copy a partial data.
Actually it should have compared with
if (strlen(testString) < sizeof(msgArray)) {
}
else {
printf("too long");
}
But i don't understand why it happened.
If i want to define to limit the characters count (including unicode (eg. chinese characters), how can i achieve to define the array? I think i can't use the char[] array.
Thanks a lot for all the responses.
My workaround solution: I finally decide to cut the strings to meet the limited bytes.