1

以下は私のコードです。VisualStudioで実行しようとしています。

#include <stdio.h>
#include <conio.h>
int main()
{
    //int i;
    //char j = 'g',k= 'c';
    struct book 
    {
        char name[10];
        char author[10];
        int callno;
    };
    struct book b1 = {"Basic", "there", 550};
    display ("Basic", "Basic", 550);
    printf("Press any key to coninute..");
    getch();
    return 0;
}

void display(char *s, char *t, int n)
{
    printf("%s %s %d \n", s, t, n);
}

関数の開始中括弧が入力されている行で再定義のエラーが発生します。

4

1 に答える 1

5

宣言する前に呼び出しますdisplay。このような場合、コンパイラは戻り型が。であると想定しintますが、戻り型は。ですvoid

使用する前に関数を宣言します。

void display(char *s, char *t, int n);
int main() {
    // ...

また、受信として宣言しますchar*が、文字列リテラルをそれに渡す(const char*)ことに注意してください。宣言を変更するか、引数を変更します。例:

void display(const char *s, const char *t, int n);
int main()
{
    // snip
    display ("Basic", "Basic", 550);
    //snap
}

void display(const char *s, const char *t, int n)
{
    printf("%s %s %d \n", s, t, n);
}
于 2012-04-21T14:46:47.017 に答える