我對使用C進行編程非常陌生,但我花了幾個學期在C++中。我剛剛開始了一項家庭作業任務,並且在我編寫的前幾行代碼中遇到了一個問題,我不確定發生了什麼。它會編譯得很好,當我運行它時,我可以輸入字符串,但是一旦我輸入,就會出現分段錯誤(核心轉儲)錯誤消息。這是我的代碼。我剛剛開始,我會更給它增添了不少並且也將執行在我的程序功能很好,但我把它在嬰兒的步驟:C編程分段錯誤(核心轉儲)錯誤
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct profile {
char *f_Name;
char *l_Name;
int age;
char *email;
char *password;
};
int main(void)
{
struct profile userOne; //creates a variable
printf("Please enter your first name: \n");
fgets(userOne.f_Name, sizeof(userOne.f_Name), stdin);
//takes input from user.
//I want to use fgets because my professor wants us to consider
//bufferoverflows
printf("%s\n", userOne.f_Name); //prints it to the screen
return 0;
}
你在調試器中運行它時發現了什麼,或者添加了一些打印件來跟蹤它到底有多遠? (提示:你確定你所有的指針都指向你允許訪問的內存嗎?) – John3136
[分割故障常見原因的最終列表]的可能重複(http://stackoverflow.com/questions/33047452/definitive-常見問題列表 - 分段錯誤) – CodeMouse92