从GCC编译C程序段错误:连接两个字符串 [英] c program segment error from GCC compiler: concatenate two strings
问题描述
以下code加薪段误差
从 GCC 编译器:
The following code raise segment error
from GCC compiler:
char *str1 = "India";
char *str2 = "BIX";
char *str3;
str3 = strcat(str1, str2);
printf("%s %s\n", str3, str1);
问题是 = STR3的strcat(STR1,STR2)
。
所以我的问题是:对于GCC编译,它禁止两个指针指向同一件事
So my question is: for GCC Compiler, it forbids two pointers pointing to the same thing?
推荐答案
您应该写什么,code-明智的,回答你的问题:
What you should have written, code-wise, answers your question:
char *str1 = "India";
char *str2 = "BIX";
//should have been
const char *str1 = "India";
const char *str2 = "BIX";
变量 STR1
和 STR2
在内存中的字符串常量印度
和 BIX
的位置。该内存的只读的。您尝试使用 strcat的
来连接这两个字符串。好吧,看看它做什么:
The variables str1
and str2
are being assigned the address in memory where the string CONSTANTS India
and BIX
are located. This memory is read-only. You are trying to concatenate these two strings using strcat
. Ok, look at what it does:
strcat(str1, str2);
/\ take this
|| copy ||
===========
add to end
这需要 STR1
指向的内存足够大,以容纳两个本身就是一个块,你到串联它字符。在字符串的情况下,这是从来没有的情况。结果
还记得我是怎么写你的code与常量
存储类说明?现在看看 strcat的
的原型:
This requires str1
to point to a block of memory big enough to accomodate both itself and the chars that you're concatenating onto it. In case of string literals, this is never the case.
Also remember how I wrote your code with the const
storage class specifier? Now look at strcat
's prototype:
char * strcat(char * destination, const char *source);
函数需要的第一个参数是一个普通的的char *
,而不是一个常量。这告诉你的功能将尝试指针指向改变内存。正如其他人所指出的那样,我上面提到的:记忆你的 STR1
来IS的只读指针指向的,因此段错误
The function expects the first argument to be a regular char *
, not a const. This tells you the function will attempt to change the memory the pointer points to. As others have pointed out, and I mentioned above: the memory your str1
pointer points to is read only, hence the segfault.
什么你可以写为:
char str1[15] = "India";
const char *str2 = "BIX";
strcat(str1, str2);
这应该工作。需要注意的是 strcat的
不检查/ prevent溢出。目标内存的大小应该足够大,以适应的strlen(源)+ strlen的(目标)+ 1
。使用 strncat函数
而不是效果会更好,仍然!它允许您指定要连接到目标串字符的最大数量:
That ought to work. Note that strcat
does not check/prevent overflow. The size of the destination memory should be big enough to accomodate strlen(source) + strlen(destination) + 1
. Using strncat
instead would be better, still! It allows you to specify a max number of chars to be concatenated onto the destination string:
size_t free_space = 15;
char str1[free_space] = "India";
const char *str2 = "BIX";
free_space -= strlen(str1) + 1;//add 1 here, to ensure we'll have room for the terminating \0 char
char *str3 = strncat(
str1,
str2,
free_space
);
//now we've concatenated something onto our string, keep track of free space left:
free_space -= strlen(str2);//dangerous, though -> size_t is unsigned, perhaps best do:
size_t src_len = strlen(str2);
char *str3 = strncat(
str1,
str2,
free_space
);
if (src_len >= free_space)
free_space = 0;
else
free_space -= src_len;
这篇关于从GCC编译C程序段错误:连接两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!