[英]Create function which copy all values from one char array to another char array in C (segmentation fault)
我有一个任务。 我必须将所有值从一个 char 数组 (sentence[]) 复制到另一个空 char 数组 sentence2[]),但我不知道为什么会出现分段错误。 他们还告诉我们,我们必须创建自己的 strlen function 来检查字符串的长度。
这是我的代码
#include <stdio.h>
#include <stdlib.h>
int new_strlen (char *tab)
{
int i;
for (i = 0; tab[i] != '\0'; ++i);
return i;
}
int copyText(char from[],char to[],int max)
{
int i, j;
if (new_strlen(from) <= max)
{
for(int i = 0; i != '\0'; i++) {
to[i] = from[i];
}
to[i+1] = '\0';
}
return 0;
}
int main (int argc, char *argv[])
{
char sentence[] = "C is \n a \n programming \t language";
char sentence2[1000];
copyText(sentence, sentence2, 1000);
printf("Show my array: %s \n", sentence2);
return 0;
}
以下是错误:
int copyText(char from[],char to[],int max)
{
int i, j; // minor problem: j is useless
if (new_strlen(from) <= max) // should be < instead of <=
{
for(int i = 0; i != '\0'; i++) { // here you declare a new i variable
// unrelated to the i declared at the beginning
to[i] = from[i];
}
to[i+1] = '\0'; // here you use again the i declared at the beginning
// which hasn't been initialized
// and i already is the index of the terminator
// therefore it should be to[i]
}
return 0;
}
此行包含两个错误:
for(int i = 0; i != '\0'; i++)
i != '\0'
等价于i != 0
。 现在你可能已经意识到你的错误了。 实际上,您需要测试from[i]
是否为 0。
to[i+1] = '\0'
:这里i
已经被for
循环递增, i
已经包含\0
终止符的索引,因此它应该是to[i] = '\0'
最后在这一行中,您使用在开头声明的i
变量 o function,其内容不确定,因为您从未为其分配任何内容,并且很可能是这一行导致分段错误: to[i+1] = '\0'
;
最后还有一个问题,如果字符串的长度是最大的,会导致问题:
if (new_strlen(from) <= max) // should be < instead of <=
如果字符串的长度是最大的,那么\0
将被放在缓冲区末尾之外,因此缓冲区溢出。
你要这个:
int copyText(char from[],char to[],int max)
{
if (new_strlen(from) < max)
{
int i;
for(i = 0; from[i] != '\0'; i++)
to[i] = from[i];
}
to[i] = '\0';
}
return 0;
}
copyText
的三个问题
i != '\0'
应该from[i] != '\0'
int i = 0
应该只是for
循环中的i = 0
不会影响另一个i
并且这样做也毫无意义。
to[i+1]
应该只是to[i]
我像你说的那样修改我的程序。 我的程序
#include <stdio.h>
#include <stdlib.h>
int new_strlen (char *tab)
{
int i;
for (i = 0; tab[i] != '\0'; ++i);
return i;
}
int copyText(char from[],char to[],int max)
{
if (new_strlen(from) < max)
{
int i;
for(int i = 0; from[i] != '\0'; i++)
{
to[i] = from[i];
}
to[i] = '\0';
}
return 0;
}
int main (int argc, char *argv[])
{
char sentence[] = "C is \n a \n programming \t language";
char sentence2[30];
copyText(sentence, sentence2, 30);
printf("Show my array: %s \n", sentence2);
return 0;
}
output
Show my array: h�ܙ�
为什么我的 output 是错误的?
我解决了你的问题。 您刚刚在 copytext() 函数的 for 循环中错过了 'form[i]'。 并使用 (new_strlen(from) <= max) 代替 (new_strlen(from) < max)。 并删除到[i+1] = '\0'; 这是不需要的。
#include <stdio.h>
#include <stdlib.h>
int new_strlen (char *tab)
{
int i;
for (i = 0; tab[i] != '\0'; ++i);
return i;
}
int copyText(char from[],char to[],int max)
{
if (new_strlen(from) <= max)
{
for(int i = 0; from[i] != '\0'; i++)
{
to[i] = from[i];
}
}
return 0;
}
int main (int argc, char *argv[])
{
char sentence[] = "C is \n a \n programming \t language";
char sentence2[1000];
copyText(sentence, sentence2, 1000);
printf("Show my array: %s \n", sentence2);
return 0;
}
我有一个任务。 我必须将所有值从一个 char 数组 (sentence[]) 复制到另一个空 char 数组 sentence2[]),
我必须复制所有值,然后是 function copyText
的第三个参数
int copyText(char from[],char to[],int max);
是多余的。 一般来说,它确实允许复制所有值。
我认为“所有值”是指存储在源数组中的字符串的所有字符。
要将字符串从一个字符数组复制到另一个字符数组,不需要计算字符串长度的 function。 它也是多余的。
function copyText
的返回类型int
没有意义。 从中复制存储字符串的字符数组应具有限定符const
。
标准 C 字符串函数遵循目标字符数组应该是第一个 function 参数并且函数应该返回指向目标字符数组的指针的约定。
在 function 中未使用声明的变量j
int i, j;
分段错误的原因是您正在使用未初始化的变量 i 来设置目标数组中的终止零字符。 那就是你声明了一个未初始化的变量 i
int i, j;
然后在其内部循环中的 if 语句中
if (new_strlen(from) <= max)
{
for(int i = 0; i != '\0'; i++) {
^^^^^^^^^
to[i] = from[i];
}
to[i+1] = '\0';
}
你又声明了一个变量 i ,它不会在循环外存活。 循环本身永远不会迭代,因为循环的条件
i != '\0'
不满意。 变量 i 由 0 初始化,并与写入为八进制字符文字的相同 0 进行比较。
所以在这个声明中
to[i+1] = '\0';
在 if 语句之前使用了我在 function 开头声明的初始化变量。
我确信你需要写的是一个类似的 ths atndard C function strcpy。
在这种情况下,程序可以如下所示
#include <stdio.h>
char * copyText( char to[], const char from[] )
{
for ( char *p = to; ( *p++ = *from++ ) != 0; ) { /* empty */ }
return to;
}
int main (void)
{
enum { N = 1000 };
char sentence[] = "C is \n a \n programming \t language";
char sentence2[N];
printf("Show my array: %s \n", copyText(sentence2, sentence ) );
return 0;
}
程序 output 是
Show my array: C is
a
programming language
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.