简体   繁体   English

fgets()多次使用时使C程序崩溃

[英]fgets() crashing C program when used more than once

I have an array declared and populated with 3 strings (Math, Physics, English). 我有一个声明的数组,并填充了3个字符串(数学,物理,英语)。 I used the fgets() to get new subject to add to the array, and this is working fine. 我使用fgets()获取新主题以添加到数组中,并且工作正常。 However, whenever I copy the same chunk of code to get another subject from the user, the program crashes. 但是,每当我复制相同的代码块以从用户那里获取另一个主题时,程序就会崩溃。

Why is this happening? 为什么会这样呢? How do I get strings from user and add it to the array? 如何从用户那里获取字符串并将其添加到数组中?

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

int main()
{
    char *subjectName[1000];
    int numOfSubj = 0;
    int numOfUserAddedSubj = 0;

    subjectName[numOfSubj] = "Math";
    numOfSubj++;
    subjectName[numOfSubj] = "Physics";
    numOfSubj++;
    subjectName[numOfSubj] = "English";
    numOfSubj++;

//int k;
//for(k=0; k<1; k++)
//{
    // add user
    printf("Enter new subject name: ");
    fgets(subjectName[numOfSubj], 50, stdin);
    numOfUserAddedSubj++;
    numOfSubj++;
//}


// add another user
/*printf("Enter new subject name: ");
fgets(subjectName[numOfSubj], 50, stdin);
numOfUserAddedSubj++;
numOfSubj++;*/



// display content of array
int i;
for(i=0; i < ((strlen(subjectName))+numOfUserAddedSubj); i++)
{
    printf("%s\n", subjectName[i]);
}


    return 0;
}

Please note that subjectName is array of type char* . 请注意, subjectNamechar*类型的数组。 Your first 3 strings defined statically. 您的前3个字符串是静态定义的。 For the string you want to get via fgets() you need to allocate memory using mallaoc first and only then call fgets() . 对于要通过fgets()获得的字符串,需要首先使用mallaoc分配内存,然后才调用fgets()

Something like: 就像是:

subjectName[numOfSubj] = (char *) malloc(50*sizeof(char));
fgets(subjectName[numOfSubj], 50, stdin);

Don't forget to free the allocated memory at the end. 不要忘记最后free已分配的内存。 Please note that you may want to consider different approaches. 请注意,您可能需要考虑其他方法。 You may statically define the array: char subjectName[1000][50] and use strcpy() to populate it with "Math", "Physics" and "English". 您可以静态定义数组: char subjectName[1000][50]并使用strcpy()为其填充“ Math”,“ Physics”和“ English”。

You have an array of 1000 pointers, but the pointers that are relevant to this problem don't point to anything. 您有1000个指针的数组,但是与此问题相关的指针没有指向任何东西。 You need to malloc some memory for the pointers before calling fgets , eg 你需要malloc之前呼吁指针一些内存fgets ,如

printf("Enter new subject name: ");
subjectName[numOfSubj] = malloc( 50 );
fgets(subjectName[numOfSubj], 50, stdin);

It's working in first case because you are initialising the char array with subject name so compiler allocating memory. 它在第一种情况下有效,因为您正在使用主题名称初始化char数组,以便编译器分配内存。

In USER case you need to allocate memory before taking input from user. 在USER情况下,您需要先分配内存,然后再从用户那里获取输入。

fgets(subjectName[numOfSubj], 50, stdin); fgets(subjectName [numOfSubj],50,stdin); //Here you passed null char array //这里传递了null char数组

so to avoid error do 所以避免出错

subjectName[numOfSubj] = malloc( 50 ); //memory allocation

 fgets(subjectName[numOfSubj], 50, stdin);

and free the memory (Which is allocated by malloc) whenever you finish the use of it . 并在每次使用完内存后free内存(由malloc分配的内存)。

free(subjectName[numOfSubj] );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM