Stack smashing
表示您正在非法使用堆栈(计算机内存的一部分)。堆栈内存的非法使用可以通过多种方式完成。一种方法是向数组添加更多的元素而不是其容量。例如,如果您尝试将15个元素添加到容量为10个元素的数组中,那么您将拥有stack smashing
。
这里你的情况,数组char arr[80];
有80
字符的容量,但是你在这个数组中添加了超过80个字符。这就是为什么你得到stack smashing
错误。
你的代码有两个问题。首先,您将以上的元素添加到arr[]
以上。
/* Issue 1 */
for(k=0;k<=4;k++)
{
if(wolf[k]!=str[i])
{
/* You are adding str[i] to
arr[] multiple times
*/
arr[j]=str[i];
j++;
}
}
这里您比较str[i]
每个元音的性格和补充说,性格arr[]
每次。这样每个字符加5次(非元音字母)或4次(元音字符)。
要解决此问题,您需要将str[i]
与所有元音进行比较,并且只有在与任何元音不匹配时才将其添加到arr[]
。有很多方法可以做到这一点。例如,您可以使用附加变量作为标志。
其次,您不检查arr[]
是否有剩余空间添加任何新字符。在您的这部分代码:
/* Issue 2: You are not checking if space left in arr[] */
for(i=0,j=0;i<len;i++)
{
在这里,你的循环条件,你需要确保有留在ARR []
空间。所以,你需要在for循环中增加一个条件。
这是一个解决方案(多间):
/* Check if space left in arr, i.e. j < 80 */
for(i=0,j=0;i<len && j < 80;i++)
{
/* Add a flag:
1 means vowel
0 means not a vowel
*/
int v_flag = 0;
for(k=0;k<=4;k++)
{
if(wolf[k] == str[i])
{
v_flag = 1; /* Indicate that this is vowel */
break;
}
}
/* Add to arr[] only if not a vowel */
if (v_flag == 0)
{
arr[j] = str[i];
j++;
}
}
/* Null terminate the string */
arr[j] = '\0';
来源
2016-08-01 08:59:20
sps
'如果(狼[K] = STR [1]!)':条件不匹配时,不必全部。例如[this](http://ideone.com/IM5k7t) – BLUEPIXY
@NamanSharma:您可以点击其分数下面的灰色复选标记来接受其中一个答案。 – chqrlie