开发者

strncpy overwrites existing character string

I've created a function to convert a number into a roman numeral. I know the logic of the conversion itself is correct, however, each time strncpy is called, it overwrites the previous value of "rom". I even tried calling it back to back and it only returned the latter.

Here's a snippet from the code:

   char* rom = (char*) calloc (10,sizeof(char));

    while(intval>=1000){
开发者_运维百科        intval -= 1000;
    strncpy(rom,"M",2);
    }

Maybe using calloc is part of the issue, but I tried using malloc and it gave me the same result.


you want to append, but strcpy just copies to the address (and overwrites). use strcat or strncat


I believe you want strcat() and not strcpy()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜