【发布时间】:2021-03-24 02:02:28
【问题描述】:
我正在尝试将 s 的子字符串复制到指向字符串数组的指针。下面是我的代码。我已经使用 malloc 分配了内存。但是当我尝试执行 strncpy 时,我得到了分段错误。如果我的代码有任何问题,谁能告诉我?或者是否允许对指向字符串的指针数组进行 strncpy 操作
s 是一个长度为 32 的字符串
char **suptr = (char **)malloc(sizeof(char *) * 11);
if(suptr != NULL)
{
strncpy(suptr[0], s, 10);
strncpy(suptr[1], s+10, 10);
strncpy(suptr[2], s+20, 10);
strncpy(suptr[3], s+30, 2);
}
提前致谢
【问题讨论】:
-
投
malloc的结果是不好的做法(没有必要)。 -
s的大小是多少? -
它的 32 位字符串
-
我的意思是字符串的长度
s -
长度为32
标签: c string pointers segmentation-fault strncpy