strncpy会覆盖现有的字符串

sta*_*tan 3 c strncpy

我创建了一个将数字转换为罗马数字的函数.我知道转换本身的逻辑是正确的,但是,每次调用strncpy时,它都会覆盖之前的"rom"值.我甚至试着背靠背调用它只返回后者.

这是代码中的一个片段:

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

    while(intval>=1000){
        intval -= 1000;
    strncpy(rom,"M",2);
    }
Run Code Online (Sandbox Code Playgroud)

也许使用calloc是问题的一部分,但我尝试使用malloc,它给了我相同的结果.

Foo*_*Bah 11

你想追加,但strcpy只是复制到地址(和覆盖).用strcatstrncat