Kochan InsertString分段错误

时间:2017-01-29 18:30:59

标签: c

我正在通过Kochan在C book中编程,我正在进行一项练习,该练习需要一个函数在另一个字符串中插入一个字符串,函数调用包括要插入字符串的位置。

我写了下面的代码,但每当我输入输入时都会收到分段错误。我认为这是因为'输入' string被定义为用户输入的长度,然后insertString函数尝试向该字符串添加其他字符。我无法看到一种将字符串定义为足够大以便能够接收其他字符的方法。你认为这就是我收到分段错误的原因吗?还有其他方法可以解决这个问题吗?

#include<stdio.h>
#include <string.h>


insertString(char input[], const char insert[], int position)
{
int i, j;
char temp[81];

j = strlen(input);

for(i = 0; i < position - 1; i++)
{
    temp[i] = input[i];
}

for(j = 0; insert != '\0'; i++, j++)
{
    temp[i] = insert[j];
}

for(j = i - j; input != '\0'; i++, j++)
{
    temp[i] = input[j];
}

for(i = 0; temp[i] != '\0'; i++)
{
    input[i] = temp[i];
}

input[i] = '\0';
}

void readLine(char buffer[])
{
char character;
int i = 0;

do
{
    character = getchar();
    buffer[i] = character;
    i++;
}
while(character != '\n');

buffer[i - 1] = '\0';
}

int main(void)
{
char input[81];
char insert[81];
int position;

printf("Enter the first string: ");
readLine(input);

printf("Enter the insert string: ");
readLine(insert);

printf("Enter placement position int: ");
scanf("%i", &position);

insertString(input, insert, position);

printf("The adjusted string is %s\n", input);

return 0;
}

4 个答案:

答案 0 :(得分:0)

也可能有其他原因,但以下片段肯定会崩溃:

for(j = 0; insert != '\0'; i++, j++)
{
    temp[i] = insert[j];
}

原因是 - 因为insert不会被增加或操纵 - 这是一个无限循环的写作&#34;无限期&#34;长期以来。一旦超过其长度80(或稍后),它将崩溃。我想你的意思是for(j = 0; insert[j] != '\0'; i++, j++),对吧?

答案 1 :(得分:0)

检查for函数中的所有insertString循环条件。例如:

for(j = 0; insert != '\0'; i++, j++)
{
    temp[i] = insert[j];
}

是无限循环。因此,您可以从temp数组边界访问内存。它会导致UB和分段错误。看起来您需要insert[j] != '\0'条件。

答案 2 :(得分:0)

我对这本书很熟悉。作者斯蒂芬科坎(Stephen Kochan)有一个网站,上面有章节练习奇数末尾的答案。

该网站位于classroomm.com,但您需要查看一些以查找信息。

以下是该网站与此练习相关的信息:

  

用C编程,练习10-7(第3版)和9-7(第4版)

     

/ *将字符串s插入到从i开始的字符串源中      此函数使用定义的stringLength函数      在本章。

     

注意:此函数假定源足够大            存储插入的字符串(危险!)* /

void  insertString (char  source[], char  s[], int  i)
{ 
int  j, lenS, lenSource;

/* first, find out how big the two strings are */

lenSource = stringLength (source);
lenS = stringLength (s);

/* sanity check here -- note that i == lenSource
  effectively concatenates s onto the end of source */

if (i > lenSource)
    return;

/* now we have to move the characters in source
  down from the insertion point to make room for s.
  Note that we copy the string starting from the end
  to avoid overwriting characters in source.
  We also copy the terminating null (j starts at lenS) 
  as well since the final result must be null-terminated */

for ( j = lenSource;  j >= i;  --j )
    source [lenS + j] = source [j];
     

/ *我们已经腾出空间,现在将其复制到源代码中         插入点* /

for ( j = 0;  j < lenS; ++j )
    source [j + i] = s[j];
}

答案 3 :(得分:0)

在insertString函数中某处出现错误,它超出了界限。顺便说一句,你的insertString函数并不以单词void开头。

如果我替换我为练习编写的insertString函数,那么该程序可以正常工作。

#include<stdio.h>
#include <string.h>

void insertString (char source[], const char s[], int start)
{
    int stringLength (const char s[]);

    int lenSource = strlen (source);
    int lenString = strlen (s);
    int i;

    if ( start > lenSource ) {
        printf ("insertion point exceeds string length\n");
        return;
    }

    // move the characters in the source string which are above the
    // starting point (including the terminating null character) to make
    // room for the new characters; to avoid overwriting characters the
    // process begins at the end of the string

    for ( i = lenSource; i >= start; --i )
        source[i + lenString] = source[i];

    // insert new characters 

    for ( i = 0; i < lenString; ++i )
       source[start + i] = s[i];
}

void readLine(char buffer[])
{
char character;
int i = 0;

do
{
    character = getchar();
    buffer[i] = character;
    i++;
}
while(character != '\n');

buffer[i - 1] = '\0';
}

int main(void)
{
char input[81];
char insert[81];
int position;

printf("Enter the first string: ");
readLine(input);

printf("Enter the insert string: ");
readLine(insert);

printf("Enter placement position int: ");
scanf("%i", &position);

insertString(input, insert, position);

printf("The adjusted string is %s\n", input);

return 0;
}