簡體   English   中英

C 數組分配問題 / CS50 分數計算器

[英]C Array Assigning Problem / CS50 Score Calculator

我嘗試用 CS50 學習 C 並嘗試編寫一個考試分數計算器。 我嘗試編寫如下動態代碼,但出現錯誤。 這是我的主要代碼:

#include <cs50.h>
#include <stdio.h>

const int total_exam = 3 ;  // Now we are creating a constant

//Prototype
//int get_scores(void);

int main(void)
{
    // get exam scores
    int *all_scores;
    all_scores = get_scores();

    printf("Your average score is %f\n", average(total_exam, all_scores) );

}

// create an function to get exam scores of the user

int *get_scores()
{
    //get exam scores of the user

    int scores[total_exam];

    for (int i=0;i< total_exam; i++)
    {
        scores[i] =  get_int("What's your exam score?: "); // each int uses 4 byte space
    }

    return scores;
}

// create an function to calculate average

float average(int length, int array[])
{
    int sum=0;
    for (int i=0 ; i<length; i++)
    {
        sum = sum+array[i];
    }

    return sum / (float) length ;
}

但是當我嘗試執行它時,出現如下錯誤。

scores_with_array.c:14:18: error: implicit declaration of function 'get_scores' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    all_scores = get_scores();
                 ^
scores_with_array.c:14:16: error: incompatible integer to pointer conversion assigning to 'int *' from 'int' [-Werror,-Wint-conversion]
    all_scores = get_scores();
               ^ ~~~~~~~~~~~~
scores_with_array.c:16:42: error: implicit declaration of function 'average' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    printf("Your average score is %f\n", average(total_exam, all_scores) );
                                         ^
scores_with_array.c:16:42: error: format specifies type 'double' but the argument has type 'int' [-Werror,-Wformat]
    printf("Your average score is %f\n", average(total_exam, all_scores) );
                                  ~~     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                  %d
scores_with_array.c:22:6: error: conflicting types for 'get_scores'
int *get_scores()
     ^
scores_with_array.c:14:18: note: previous implicit declaration is here
    all_scores = get_scores();
                 ^
scores_with_array.c:33:12: error: address of stack memory associated with local variable 'scores' returned [-Werror,-Wreturn-stack-address]
    return scores;
           ^~~~~~
scores_with_array.c:38:7: error: conflicting types for 'average'
float average(int length, int array[])
      ^
scores_with_array.c:16:42: note: previous implicit declaration is here
    printf("Your average score is %f\n", average(total_exam, all_scores) );
                                         ^
7 errors generated.

我想這是關於數組分配,但我無法理解。 你能幫忙解決這個問題嗎? 提前致謝。

  1. 為什么你的get_scores原型get_scores評論了? 它導致了隱式聲明問題。 而且也是錯的,應該是int *get_scores(void); .
  2. average函數的原型不存在。 C 假定它返回一個int ,這會導致更多問題。
  3. 為什么要返回scores數組,函數退出后,堆棧會被破壞並且可能會發生內存泄漏。 像這樣分配它: int *scores = malloc(sizeof(int) * total_exam)

你的代碼有點亂,所以如果進一步的問題仍然存在,我不會感到驚訝。 此外,請隨時通過評論要求澄清。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM