strncpy or strlcpy in my case

前端 未结 6 1283
执笔经年
执笔经年 2020-12-10 11:21

what should I use when I want to copy src_str to dst_arr and why?

char dst_arr[10];
char *src_str = \"hello\";

P

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 11:37

    you should not use strncpy and not strlcpy for this. Better you use

    *dst_arr=0; strncat(dst_arr,src_arr,(sizeof dst_arr)-1);
    

    or without an initialization

    sprintf(dst_arr,"%.*s",(sizeof dst_arr)-1,src_arr);
    

    dst_arr here must be an array NOT a pointer.

提交回复
热议问题