評價: 0 回應: 1 閱覽: 128
置頂

錯誤處理函式的用法

各位好~~

這是我從書上照抄的程式,想練習錯誤處理

不過不知道為何compile後會有warning
ferror.c:13:16: warning: initialization makes pointer from integer without
a cast
     char *es = strerror(errno);
                ^

還有執行後會有Segmentation fault

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

int main(void) {
    FILE *fp;
    puts("Open a file nofile.xxx");
    fp = fopen("nofile.xxx","r");
    if (!fp) {
        perror("Error!");
    }

    char *es = strerror(errno);
    puts(es);

    clearerr(stdin);

    return 0;

}

 

熱門回應

#include <string.h>

你可以不用先宣告. 但預設的 prototype 可能不對

一般的函式會有連結錯誤. 可是這例子會自動連結標準函式庫

預設的 prototype 跟連結的函式庫不一致造成未定義行為

會員登入 (先登入會員才能回覆留言喔!)

Facebook留言