C函数中的SIGSEGV

时间:2015-05-20 16:04:35

标签: c

我是C的新手,我在练习时遇到麻烦: 输入文本和字符,使用函数struct info find(char *,int,char) 它返回一个结构,其中包含文本中第一个出现的地址 它出现在文本中的次数。尝试使用它时,我收到SIGSEGV错误。我用明显的问题标记了这些行:

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

struct info{
    char *ret;
    int length;
};

char * load(int *);
struct info find(char *, int, char);

int main(void)
{
    char *TP;
    char character;
    struct info SP;
    int times;

    TP=load(&times);                        //loads the text
    printf("Enter the character to find");
    scanf("%c", &character);
    //finds a character, and returns a struct   with the address of the first
    //occurrence in the text and the amount of times it appears in the text
    SP=find(TP, times, character);

    if(SP.ret!=NULL)
    {
        printf("%s", SP.ret);
        printf("The length of the displayed text is %d", SP.length);
    }
    else
    {
        printf("Character doesn't exist in the text");
    }

    return(0);
}

char * load(int *times)
{
    static char text[1000];
    printf("Enter text, max 1000 chars\n");
    while((*times<1000)&&(((text[*times])=getchar())!=EOF))
    {
        *times++;                           //call stack here
    }
    text[*times]='\0';                      //call stack here
    return(text);
}

struct info find(char *TP, int cant, char character)
{
    static struct info aux;
    aux.ret=strchr(TP,character);
    aux.length=strlen(aux.ret);
    return(aux);
}

1 个答案:

答案 0 :(得分:-1)

您可能正在尝试访问数组元素超出范围或尝试使用太多内存。