2016-08-15 153 views
0
#include <stdio.h> 
#include <string.h> 
struct students{ 
    char name[50]; 
    int age; 
    int height; 
}; 

int main(int argc, char **argv) 
{ 
    struct students manoj; 

    strcpy(manoj.name, "manojkumar"); 
    manoj.age = 15; 

    displaymanoj(&manoj); //print testing \n , name , age 

    return 0; 
} 

void displaymanoj(struct students *ptr) { 
    printf("Testing...............DEBUG\n"); 
    printf("%s\t%d\n", ptr->name,ptr->age); 
    printf("END OF TEST: SUCESS -manoj-"); 
} 

我正在學習C,它正在使用指針指向結構變量的地方。我在運行程序時得到正確的輸出。只是我的Geany IDE發出了一些我想知道爲什麼的消息。53:6:警告:函數衝突類型

我的編譯器的消息如下:

回答

2

您必須稱他們之前聲明的功能。

所以,你的程序應該是這個樣子

// Includes 
// Structure 

// Function prototype declaration 
// This was what you were missing before 
void displaymanoj(struct students *ptr); 

int main(int argc, char **argv) 
{ 
    ... 
} 

void displaymanoj(struct students *ptr) { 
    ... 
} 
1

既然你的displaymanoj()定義當你從main()調用它是沒有看到,編譯器隱式聲明一個返回類型爲int 與實際衝突一。請注意,自C99標準以來,隱式聲明已被刪除且不再有效。

爲了修正它:

1)無論是移動上述主要的()的定義或
2功能displaymanoj())執行displaymanoj()forward declaration

+0

是的,我已經通過上面的主要聲明來解決它。什麼是'implcit聲明'的實際意義 – ManojKumar

+0

我是16,並且是新的c編程 – ManojKumar

+1

當編譯器第一次看到一個函數,並且如果它還沒有看到該函數的聲明,那麼編譯器會隱式聲明一個'int'作爲返回類型。這被稱爲隱式函數聲明,因爲編譯器會爲你做。這是C99之前的舊規則。在C99中,這條規則已被刪除,現在所有函數都需要聲明(定義也提供聲明 - 這就是移動main()函數上面的函數的原理)。 –