簡體   English   中英

為什么我的代碼會收到此消息“分段錯誤(核心轉儲)”

[英]why my code is getting this message “Segmentation fault (core dumped)”

我正在嘗試在 Linux 中編寫此 c 程序,但是當我運行該程序時,它執行了一半的代碼,然后 bash 在此處顯示此消息“Segmentation 錯誤”

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

int main()
{   
    char stName[30];
    int info[3];
    int showinfo()
   {
    printf("The name of the student is %s\n",stName);
    printf("The age of the student is %d",info[0]);
    int i = 1;
    while(i<4)
    {
     printf("The marks in subject %d is %d",i,info[i]);
     i++;
    }
   }
    int getinfo()
   {
    printf("Enter name of the student: ");
    gets(stName);
    printf("enter the age of the student %s : ",stName);
    gets(info[0]);
    for(int i=1;i<4;i++)
    {
     printf("Enter the marks in subjet %d",i);
     gets(info[i]);
    }
    return 0;
   }
    getinfo();
    showinfo();
    
}

output是這樣的:

Enter name of the student: Keshav
enter the age of the student Keshav : 20
Segmentation fault (core dumped)

你有很多小問題。 首先"ISO C forbids nested functions" ,您應該在main() ) 上方聲明/定義void showinfo()char *getinto() ) ,而不是在其中。 接下來,您必須確保在循環時不要嘗試超出 arrays 的范圍進行寫入。 接下來,請參閱: 為什么 gets() 如此危險,永遠不應該使用! 它非常不安全,而且很容易被緩沖區溢出利用,它已被 C11 庫完全刪除。

在編寫showinfo()時,它只不過是一個 output 例程,沒有必要讓 function 返回一個值。 將返回類型更改為void以明確這一點。 由於您現在在main()之外聲明函數,因此您需要將 function 中所需的變量作為參數傳遞,例如

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

#define MAXC 128   /* if you need a constant, #define one (or more) */
#define NFOC   4

void showinfo (const char *stName, int *info)
{
    printf ("\nThe name of the student is %s\n"
            "The age of the student is %d\n\n", stName, info[0]);
    
    for (int i = 1; i < NFOC; i++)
        printf ("  The marks in subject %2d is %2d\n", i, info[i]);
}

注意:您只需要對 output 兩行文本進行一次printf()調用。在編譯期間將連接相鄰的文字字符串)

對於getinfo()

char *getinfo (char *stName, int *info)
{
    fputs ("Enter name of the student: ", stdout);
    if (fgets (stName, MAXC, stdin) == NULL)
        return NULL;
    stName[strcspn (stName, "\r\n")] = 0;   /* trim trailing \n from end of stName */
    
    printf ("enter the age of the student %s  : ", stName);
    if (scanf ("%d", &info[0]) != 1)
        return NULL;
    
    for (int i = 1; i < NFOC; i++) {
        printf ("Enter the marks in subjet %d : ", i);
        if (scanf ("%d", &info[i]) != 1)
            return NULL;
    }
    return stName;
}

上面的stName arrays 以及info數組作為參數傳遞給 function 以及上面的showinfo() 這里返回stName是為了方便,如果需要,您可以在printf變量列表中使用 function。 它返回NULL以指示收集輸入失敗。

main()現在簡化為:

int main (void)
{
    char stName[MAXC];
    int info[NFOC];
    
    getinfo (stName, info);
    showinfo (stName, info);
}

示例使用/輸出

運行您的程序並在出現提示時提供輸入,您將收到以下 output:

$ ./bin/nested
Enter name of the student: John Q. Student
enter the age of the student John Q. Student  : 21
Enter the marks in subjet 1 : 88
Enter the marks in subjet 2 : 87
Enter the marks in subjet 3 : 92

The name of the student is John Q. Student
The age of the student is 21

  The marks in subject  1 is 88
  The marks in subject  2 is 87
  The marks in subject  3 is 92

啟用警告

始終在啟用警告的情況下進行編譯,並且在沒有警告的情況下編譯之前不要接受代碼。 要啟用警告,請將-Wall -Wextra -pedantic添加到您的gcc/clang編譯字符串(也可以考慮添加-Wshadow以警告陰影變量)。 對於VS (Windows 上的cl.exe ),使用/W3 所有其他編譯器將具有類似的選項。 閱讀並理解每個警告——然后 go 修復它。 他們將識別任何問題,以及它們發生的確切線路。 通過聆聽編譯器告訴您的內容,您可以學到很多東西。

如果您還有其他問題,請仔細查看並告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM