2016-02-04 36 views
1

啓用鑄當我編譯此代碼:賦值時將整數指針沒有默認

void rep_and_print(char * str, char * patt, int l, int i) 
{ 
    char * pch; // pointer to occurence 
    char * s; 
    s = str; // save original pointer 
    if (i == 0) 
    { 
     while ((pch = strstr(str,patt)) != NULL) 
     { 
      // FOUND 
      memset (pch,'*',l); // Set asterisk 
      str = pch + l; // move pointer to the end of asterisks to found new occurences 
     } 
    } 
    else 
    { 
     while ((pch = strcasestr(str,patt)) != NULL) 
     { 
      // FOUND 
      memset (pch,'*',l); // Set asterisk 
      str = pch + l; // move pointer to the end of asterisks to found new occurences 
     } 
    } 
    printf ("%s",s); 
} 

我得到這個錯誤:

warning: assignment makes pointer from integer without a cast [enabled by default]

while ((pch = strcasestr(str,patt)) != NULL) 

,並有一個箭頭指向等號pchstrcasestr

+1

當你使用'-Wall'編譯時,你還會收到警告嗎? 'strcasestr'不是一個標準函數,在''中可能沒有定義,所以你的編譯器假定它返回一個'int'。這將解釋爲什麼對於'strstr'的​​相同代碼,它不是一個錯誤。 –

回答

2

從手冊頁:

#define _GNU_SOURCE 

    #include <string.h> 

    char *strcasestr(const char *haystack, const char *needle); 

您需要添加#define _GNU_SOURCE之前#include <string.h>(和#include <stdio.h>以及)爲了使函數聲明可見。

0

,而((PCH = strcasestr(STR,PATT))!= NULL) strcasestr是一個非標準的擴展,所以你必須在你的文件的第一行添加的#define _GNU_SOURCE,或-D_GNU_SOURCE編譯。

相關問題