[英]Copying arrays in C
In my C program I am trying to copy an array of char's to another array whilst removing the first element (element 0). 在我的C程序中,我试图在删除第一个元素(元素0)的同时将一个char数组复制到另一个数组。
I have written: 我已经写了:
char array1[9];
char array2[8];
int i, j;
for(i = 1, j = 0 ; i < 10, j < 9; i++, j++){
array2[j] = array1[i];
}
printf(array2);
When I print array2, it gives me a stack overflow. 当我打印array2时,它给了我一个堆栈溢出。
Any ideas? 有任何想法吗?
Two issues: First, when printing a string with printf
, and working with other standard C string functions in general, your char arrays need to be null-terminated so the functions know where the string ends. 有两个问题:首先,当使用
printf
打印字符串时,并且通常与其他标准C字符串函数一起使用时,您的char数组需要以空值结尾,以便函数知道字符串的结尾。 You are also writing one past the end of your arrays. 您还要在数组末尾写一个。
Second, when using printf
, it is almost always a bad idea to use the string you want to print as the format string. 其次,在使用
printf
,将要打印的字符串用作格式字符串几乎总是一个坏主意。 Use 采用
printf("%s", array2);
instead. 代替。 If you use
printf
as in the original example and array2 can be influenced by the user, then your program is likely vulnerable to a format string vulnerability. 如果您像原始示例中那样使用
printf
,并且array2可能会受到用户的影响,则您的程序可能容易受到格式字符串漏洞的攻击。
Your string isn't null-terminated, so when its printed it continues printing characters past the 8 you've allocated looking for one but runs out of stack space before then. 您的字符串不是以Null结尾的,因此在打印字符串时,它将继续打印超过您分配的8个字符的字符,以寻找一个,但在此之前堆栈空间不足。 You're also writing to one character more than you've allocated and your conditions should be "combined" with
&&
-- a ,
ignores the result of the first expression. 您还要写入一个比分配的字符更多的字符,并且您的条件应与
&&
“组合” &&
,
忽略第一个表达式的结果。 You should also avoid using a string variable as the string formatter to printf
. 您还应该避免将字符串变量用作
printf
的字符串格式化程序。
Here's your code fixed : 这是您固定的代码:
char array1[10] = "123456789";
char array2[9];
int i, j;
for(i = 1, j = 0 ; i < 10 && j < 9; i++, j++){
array2[j] = array1[i];
}
printf("%s\n", array2);
You can also simplify the loop by using a single index variable i
and indexing array2
with i+
. 您还可以通过使用单个索引变量
i
和带有i+
索引array2
来简化循环。 You can also remove the loop entirely by using strncpy
, but be aware that if n
is less than the length of the string + 1 it won't add a null-terminator. 您也可以使用
strncpy
完全删除循环,但是请注意,如果n
小于字符串的长度+ 1,则不会添加空终止符。
Use memcpy(): 使用memcpy():
memcpy( array2, &array1[1], 8 );
Thats easier. 那更容易。
不必使用额外的array2
printf("%.8s",array1+1);
When you say printf(array2)
, it thinks it's printing a null-terminated string. 当您说
printf(array2)
,它认为它正在打印一个以空值结尾的字符串。 Since there is (possibly) no \\0
in array2
, printf
continues on past the end of array2
, wandering into memory it isn't supposed to. 由于
array2
(可能)没有\\0
,所以printf
继续经过array2
的末尾,这是不应该的,它会进入内存。
To further expand on marcog's answer: you are declaring array1 with 9 elements, 0-8, and then writing from 0-9 (10 elements). 为了进一步扩展marcog的答案:您要声明具有1个元素(0-8)的array1,然后从0-9(10个元素)写入。 Same thing with array2.
与array2相同。
Just use strcpy()
(if they're both strings!) strcpy()
wants a pointer to the source and a pointer to the destination. 只需使用
strcpy()
(如果它们都是字符串!) strcpy()
一个指向源的指针和一个指向目标的指针。 If you want to skip the first element of the source array just pass source + 1
: 如果要跳过源数组的第一个元素,只需传递
source + 1
:
char source[] = "ffoo";
char dest[] = "barbar";
strcpy(dest, source + 1);
// now dest is "foo" (since the ending \0 is copied too)
printf("\n%s\n", dest);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.