2016-11-22 5 views
0

私はここで答えを探しました。私はいくつかの関連するポストを見ましたが、私はそれをすべて理解することができませんでした.Cに新しいもので構造体を扱っています。私は、構造体がどのように処理されるかの結果としてエラーが発生するとは確信しています。構造体に問題がありますが、なぜこのエラーが発生しますか?

これは私が私のコードをコンパイルしようとしたとき、私はそれのパラメータリスト内

30 38 C宣言

30 38 C:\Users\PCPCPCPC\Documents\clock.c [Warning] 'struct dateAndtime'  

以下のコードを追加します取得エラーです:\ユーザーはPCPCPCPC \ドキュメント\を\ clock.c [警告]スコープはこの定義または宣言だけです。これはおそらくあなたが望むものではありません。

42 20 C:\ Users \ PCPCPCPC \ Documents \ clock.c [エラー]仮パラメータの型が不完全です

48 38 C:¥Users¥PCPCPCPC¥Documents¥clock.c [警告] 'struct dateAndtime'がパラメータリスト内で宣言されました

48 50 C:¥Users¥PCPCPCPC¥Documents¥clock.c [エラー]パラメータ1 ( '現在')不完全型を持つ

コード

#include <stdio.h> 
#include <stdbool.h> 

//Global struct variables 
struct date 
{ 
    int month; 
    int day; 
    int year; 
}; 

struct time 
{ 
    int seconds; 
    int minutes; 
    int hours; 
}; 

struct dateAndTime 
{ 
    struct date sDate; 
    struct time sTime; 
}; 

//prototypes 
int numberOfDays (struct date d); 
bool isLeapYear (struct date d); 
struct date dateUpdate(struct date today); 
struct time timeUpdate(struct time now); 
struct dateAndTime clocKeeper(struct dateAndtime current); 

int main() 
{ 
    //struct dateAndTime dt, future; 
    /*printf("please enter the correct date and time"); 
     printf("mm/dd/year hr:mm:sc"); 
    scanf("%i %i %i %i %i  %i",dt.sDate.month,dt.sDate.day,dt.sDate.year,dt.sTime.hours,dt.sTime.minutes,dt.sTime.seconds);*/ 

    struct dateAndTime dt1 = {{12, 31, 2004}, {23, 59, 59}}; 
    struct dateAndTime dt2 = {{2, 28, 2008}, {23, 59, 58}}; 
    //future = clocKeeper(dt1); 

    dt1 = clocKeeper (dt1); 

    printf("the updated date is %i/%i/%i ", dt1.sDate.month, dt1.sDate.day, dt1.sDate.year % 100); 
    printf("the updated time is %.2i:%.2i:%.2i", dt1.sTime.hours,dt1.sTime.minutes,dt1.sTime.seconds); 
    } 

struct dateAndTime clocKeeper(struct dateAndtime current) 
{ 
    current.sTime = timeUpdate(current.sTime); 

    if (current.sTime.hours == 0 && current.sTime.minutes == 59 && current.sTime.seconds == 59) 
    current.sDate = dateUpdate(current.sDate); 

    return current; 
} 

struct time timeUpdate(struct time now) 
{ 
    ++now.seconds; 
    if (now.seconds == 60) { // next minute 
     now.seconds = 0; 
     ++now.minutes; 
    if (now.minutes == 60) { // next hour 
     now.minutes = 0; 
     ++now.hours; 
    if (now.hours == 24) // midnight 
     now.hours = 0; 
    } 
} 
    return now; 
} 

// Function to find the number of days in a month 
int numberOfDays (struct date d) 
{ 
    int days; 
     bool isLeapYear (struct date d); 
     const int daysPerMonth[12] ={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 
    if (isLeapYear (d) == true && d.month == 2) 
      days = 29; 
    else 
      days = daysPerMonth[d.month - 1]; 

    return days; 
    } 

    // Function to determine if it's a leap year 
    bool isLeapYear (struct date d) 
    { 
    bool leapYearFlag; 
    if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0) 
      leapYearFlag = true; // It's a leap year 
    else 
      leapYearFlag = false; // Not a leap year 
    return leapYearFlag; 
} 

struct date dateUpdate(struct date today) 
{ 
     struct date tomorrow; 
     int numberOfDays (struct date d); 
    if (today.day != numberOfDays (today)) { 
     tomorrow.day = today.day + 1; 
     tomorrow.month = today.month; 
      tomorrow.year = today.year; 
    } 
    else if (today.month == 12) { // end of year 
     tomorrow.day = 1; 
     tomorrow.month = 1; 
     tomorrow.year = today.year + 1; 
} 
else { // end of month 
    tomorrow.day = 1; 
    tomorrow.month = today.month + 1; 
    tomorrow.year = today.year; 
} 
    return tomorrow; 
} 
+1

[Cでネストされた関数]の可能な重複(http://stackoverflow.com/questions/2608158/nested-function-in-c) – LPs

+0

C大文字と小文字を区別します!入力ミスを確認する前に! – Olaf

+0

私はチェックして、大文字と小文字の間違いを見ることはできません。また、これはCのネストされた機能とは異なります。 – Oghenebrume

答えて

0

あなたはclocKeeper()関数に引数として間違った構造名を可決しました。

struct dateAndTime clocKeeper(struct dateAndtime current) 

正しい方法である: -

struct dateAndTime clocKeeper(struct dateAndTime current) 
+0

5時間の監督に感謝しました。 – Oghenebrume

+0

それは私の喜びです。私はあなたの代わりに、完全な構造体名を使用することをお勧め、typedefエイリアスを使用します。 –

+0

大丈夫ですが、構造のコンセプトには新しいですが、私はそれを読んでいます – Oghenebrume

関連する問題