由C中的fgets()引起的分段错误

顶帆

这是我不了解的问题-我fgets()在main中使用,并且可以正常工作。我在函数中以完全相同的方式使用它(我认为),但出现错误[细分故障核心已转储-退出代码139)。

该代码基于Ivor Horton的“ Beginning C”一书中的示例程序(这是一个旧的图块,但我只是想从中学习基础知识)。

我的程序如下。我正在使用Geany(基本上是使用GCC进行编译)来研究* nix。您可以看到它fgets在main起作用(输出是您输入的字符串)。但是它在函数中不起作用str_in()它可以到达第二条printf()语句,然后再输入一个字符串。请注意,在本书中,霍顿使用gets()我试图在这里实现一个更安全的字符串输入函数,但是没有任何乐趣。

顺便提一下,程序应该对存储在字符串指针数组中的字符串进行排序。

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

#define TRUE 1
#define FALSE 0
#define MAX_NUM_STRINGS 50


int str_in(char **);              /*Pointer to a string pointer*/
void str_sort(char *[], int n);   /*Array of pointers to strings, number of strings in array*/
void str_out (char *[], int n);   /*Array of pointers to strings, number of strings in array*/


int main(){

    char *pS[MAX_NUM_STRINGS] = { NULL };                  /*Array of pointers to strings stored in str_space*/
    int numStrings = 0;                                     /*Count of strings*/
    char buffer[BUFSIZ];

    printf("Enter a string\n");
    fgets(buffer, BUFSIZ, stdin);
    printf("%s", buffer);
    printf("fgets works here\n\n");


    /* get string input from user - a pointer to each string is saved in pS */
    while ( str_in(&pS[numStrings]) && numStrings < MAX_NUM_STRINGS)
        numStrings++;
    if ( numStrings > 0 ){
        str_sort(pS, numStrings);
        str_out(pS, numStrings);
    }

    return 0;
}


    int str_in(char** pString){

        char buffer[BUFSIZ];
        char *p;

        printf ("Enter string:\n");

        fgets(buffer, 60, stdin);
        printf("fgets doesn't work here!!\n");

        if( buffer != NULL ){
            printf("here");
            if ((p = strchr(buffer, '\n')) != NULL)
                *p = '\0';                                  /*replace newline with null character*/
            else
                return FALSE;
            if ( strlen(buffer) > 0 ){
                strcpy(*pString, buffer);
                return TRUE;
            }
            else
                return FALSE;                               /*blank line - end of input*/
        }
        else
            return FALSE;

    }


    void str_sort(char* pStrings[], int n){
    /*sort strings by manipulating array of string pointers*/

        char *temp;
        int sorted = FALSE;
        int i = 0;

        while (!sorted){

            sorted = TRUE;
            for(i = 0; i < n - 1; i++){
                temp = pStrings[i];
                if ( strcmp(temp, pStrings[i+1]) > 1 ){
                    pStrings[i] = pStrings[i+1];
                    pStrings[i+1] = temp;
                    sorted = FALSE;
                    break;
                }
            }

        }

    }


    void str_out(char* pStrings[], int n){
    /*print strings to standard output.  Free memory as each string is printed */

        int i = 0;

        printf("Sorted strings:\n");
        for(i = 0; i < n; i++){
            printf("%s", pStrings[i]);
            free(pStrings[i]);
        }

    }
Yu Hao

分割错误不是由引起的fgets(),而是由引起strcpy()

strcpy(*pString, buffer);

您尝试写入*pString,但从未为其分配内存。pSinmain()只是一个空指针数组。

另一件事是使用进行测试if( buffer != NULL ),因为buffer数组是指针,而不是指针,所以永远不会成立

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章