Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用malloc复制字符串_C - Fatal编程技术网

如何使用malloc复制字符串

如何使用malloc复制字符串,c,C,我有一个练习,我需要使用malloc复制一个字符串,我不确定我的错误在哪里(我有一个分段错误) 此部分错误sizeof(*str)。您需要找到字符串的长度,而不是第一个字符的内存大小(这就是所做的)您应该malloc字符串所包含的字符数。所以马洛克会 str2 = malloc(sizeof *str2 *(strlen(str)+1)); 还要检查malloc的返回值 str2=malloc(sizeof *str2 *(strlen(str)+1)); if (!str2){ //

我有一个练习,我需要使用malloc复制一个字符串,我不确定我的错误在哪里(我有一个分段错误)


此部分错误
sizeof(*str)
。您需要找到字符串的长度,而不是第一个字符的内存大小(这就是所做的)

您应该
malloc
字符串所包含的字符数。所以马洛克会

str2 = malloc(sizeof *str2 *(strlen(str)+1));
还要检查
malloc
的返回值

str2=malloc(sizeof *str2 *(strlen(str)+1));
if (!str2){
   // memory allocation failed.
}
而且你在循环中做了一些错误的事情

while(*str!='\0'){ // not `str` rather the `*str` you want to check.
    *str2 = *str;
     str  =  str +1;
     str2 =  str2+1;
}
简言之,你能做到

while(*str){
    *str2++ = *str++;
}
您还返回了指向
\0
str2
。您需要将分配内存的开头存储在某个位置,以便可以返回它

str2 = malloc ..
if( !str2 ){
   // error
}
char *ret = str2; // storing it so that you can return it later.
..
..
return ret;

将所有大写名称都用作函数名是不好的做法。您可以将其命名为
my_strdup()
或其他有意义的名称

您需要找到
malloc
的字符串长度

str2=malloc(sizeof *str2 *(strlen(str)+1));
if (!str2){
   // memory allocation failed.
}
用电话

str2=malloc(strlen(str) + 1); // +1 for the null character to terminate the string

什么是
IMPLEMENT(duplicateString)
应该做什么?请下次缩进您的代码,使用所有警告和调试信息进行编译,即
gcc-Wall-Wextra-g
使用调试器
gdb
@BasileStarynkevitch我不知道如何实现它。你知道我能从哪里学到什么吗?我不知道你想实现什么。
gcc
gdb
都是自由软件工具(可在许多操作系统上使用),可在命令行上使用。(我不知道您使用的是什么操作系统;我强烈建议您先安装,然后使用一些Linux发行版,因为它对开发人员和学生都非常友好)也许
while(*str)
str++
amd
str2++@EdHeal.:已编辑..是的,它会更干净谢谢你的帮助和快速回答,我在你更新答案的同时更新了我的代码,效果非常好。我正在学习使用malloc,指针。。。这不简单@createurx使其更简单。例如,如果您使用了“int stringLength=sizeof(*str);”,“str2=malloc(stringLength);'并且打印出stringLength(或者通过检查它的值),您会立即看到您只定位了一个字节。