c - 当 dest 小于 src 但大到足以容纳 src 的所需子字符串时,为什么 strncpy() 会产生垃圾?

标签 c string strcpy strncpy

我尝试使用 strncpy() 限制复制到 dest(此处为 str1)的 n 字节数。 dest 足够大,可以容纳 n 个字节,但是当 dest 小于源(此处为 argv[1])时,输出会产生垃圾。 当我使 dest 足够大以容纳源时,这看起来有所不同。

代码如下:

#include <stdio.h>                                                              
#include <string.h>                                                             
                                                                                
int main(int argc, char *argv[])                                                
{                                                                               
/* // Works when str1[?] is 20:                                                                    
  char str1[20];                                                                
  char str2[20]; */                                                             
                                                                                
  // produces garbage, str1 is smaller than argv[1] but big enough for the 4 bytes copied                                                            
  char str1[10];                                                                
  char str2[10];                                                                
                                                                                
  printf("argc = %i\n", argc);                                                  
  if (argc <= 1 || argc > 2){                                                   
    printf("Input Example:\n"                                                   
           "  %s SEGMENT:OFFSET\n"                                              
           "  %s 0100:F00A\n", argv[0], argv[0]);                               
    return 0;                                                                   
  }                                                                             
  printf("strlen(argv[1]) = %li, argv[1] = %s\n"                                
          , strlen(argv[1]), argv[1]);                                          
                                                                                
  // str1                                                                       
  strncpy(str1, argv[1], 4); // copying 4 bytes to str1                                                   
  printf("Copy only 4 Bytes -> sizeof(str1) = %li, "                            
         "strlen(str1) = %li, str1 = %s\n", sizeof(str1), strlen(str1), str1);  
                                                                                
  // str2                                                                       
  strncpy(str2, argv[1], 3); // copying 3 bytes to str2                                                   
  printf("Copy only 3 Bytes -> sizeof(str2) = %li, "                            
         "strlen(str2) = %li, str2 = %s\n", sizeof(str2), strlen(str2), str2);  
                                                                                
  return 0;                                                                     
}                      

0100:F00A 的输入产生:

./test.bin 0100:F00A
argc = 2
strlen(argv[1]) = 9, argv[1] = 0100:F00A
Copy only 4 Bytes -> sizeof(str1) = 10, strlen(str1) = 8, str1 = 0100�U
Copy only 3 Bytes -> sizeof(str2) = 10, strlen(str2) = 3, str2 = 010

预计只是 str1 = 0100

我也想知道为什么 str2 是正确的,它的初始数组大小和 str1 一样小。

当我改变时

char str1[10] 

char str1[20]

并通过这样做使其大于 argv[1] 的输入,那么输出是正确的:

./test.bin 0100:F00A
argc = 2
strlen(argv[1]) = 9, argv[1] = 0100:F00A
Copy only 4 Bytes -> sizeof(str1) = 20, strlen(str1) = 4, str1 = 0100
Copy only 3 Bytes -> sizeof(str2) = 20, strlen(str2) = 3, str2 = 010

似乎 strncpy 是先将所有内容复制到 dest,然后再删除其余部分。但这就是 strncpy 的工作方式吗? 我认为它只是复制需要的 4 个字节。

最佳答案

来自手册页 https://linux.die.net/man/3/strncpy

The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

即在您的情况下,不会将 null 放在 dest 中

关于c - 当 dest 小于 src 但大到足以容纳 src 的所需子字符串时,为什么 strncpy() 会产生垃圾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72917964/

相关文章:

c - 覆盖文件中的一行

c - 在关联数组中查找此数据的最佳算法?

c - 数组到指针与二维数组

java - 按空格和破折号拆分字符串,但不按字符串的值拆分字符串

c++ - 我的 strcpy_s 不能与我的 char *pointer 一起使用,为什么?

c++ - 我们应该使用什么作为 std::strcpy 的第二个参数?

C对数组的厌恶

python - Python string.maketrans ("","") 是做什么的?

string - SQL逗号分隔的字符串总数

C : how do I printf "the square root of 1764 is 42 and * in ascii"?