【发布时间】:2014-01-04 14:56:25
【问题描述】:
问题是我为什么要定义字符串的大小(string[] 应该是string[some-number])
当程序如下时,它给了我Abort trap: 6:
#include <stdio.h>
#include <string.h>
int main(void)
{
char buffer1[] = "computer";
char string[]="program";
strcat( buffer1, string );
printf( "buffer1 = %s\n", buffer1 );
}
这是来自http://www.tutorialspoint.com/cprogramming/c_data_types.htm 的程序,它运行良好:
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
/* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}
什么是错误?即使我在程序中定义了所有字符串的大小,我的代码仍然给出Abort trap:6?
【问题讨论】: