无法将数组传递给函数。为什么不工作?

时间:2017-07-03 12:03:04

标签: c

我是C编程的初学者,我需要帮助。 这个c代码没有错误也没有任何警告,但我似乎无法让数组将值传递给calcMagicNumber函数。我该如何解决? 在此先感谢!!

#include <stdio.h>

//function prototypes
void printHeader ();                   
void askForNumber();                    
int calcMagicNumber(int num[], int size ); 
void writeResults(char*, int, int);    
void goodbye();                        

int main (){
    //assign the variables
    char name[51];                    //for user to input name
    char option='Y';                  //option for user to continue the program
    int magicNumber=0;                //to calculate the magic number
    int flag=0;      //to determine if the magic number is an even or odd number
    int num[5]={0,0,0,0,0};                 //for user to input the 5 numbers

    printHeader();

    puts("Please enter your name");
    scanf("%[^\n]s", name);


    do{

        //call the function calcMagicNumber to produce the magic number
        magicNumber = calcMagicNumber(num, 5);

        //determine if the magic number is an even number
        if(magicNumber%2==0){
            flag=1;
        }
        else{
            flag=0;
        }

        //call function writeResults to print out the user's name, the magic 
number and whether the magic number is even
        writeResults(name, magicNumber, flag);

        fflush(stdin); //clear buffer

        //ask user if he or she would like to go again
        puts("Would you like to go again? [y/n]");
        scanf("%c", &option);

        option = toupper(option); //to convert the user's input into capital 
letters

    }while(option == 'Y');

    goodbye();

    return 0;
}

//function bodies
void printHeader (){
    //print out name and chapter 3 - magic number
    printf("Chapter 3- Magic Number \n");
    printf("Tan Su Fern\n\n");
}

void askForNumber(){
    int i=1;
    int num[5]={0,0,0,0,0};

    //ask user to input number
    for(i=1;i<=5;i++){

        puts("Please enter a number.");
        scanf("%d", &num[i]);   

    }
    num[i]=i;
    printf("\n%d, %d, %d, %d, %d", num[1], num[2], num[3], num[4], num[5]);
}

int calcMagicNumber(int num[], int size ){
    int magicNumber=0;

    //call askForNumber function for user to input 5 numbers
    askForNumber();

    printf("\n%d, %d, %d, %d, %d", num[1], num[2], num[3], num[4], num[5]);
    //calculate the magic number based on the numbers input by the user
    magicNumber = (num[1]*num[5])-(num[2]+num[3]+num[4]);

    return magicNumber;
}

void writeResults(char* name, int magicNumber, int flag){
    //print out the magic number and the user's name
    printf("\nHi %s , your magic number is %d.\n", name, magicNumber);

    //print out if the magic number is an even or odd number
    if(flag==1){
        printf("This is an even number.\n\n");
    }
    else{
        printf("This is an odd number.\n\n");
    }
}

void goodbye(){
    //say goodbye
    printf("goodbye");
}

1 个答案:

答案 0 :(得分:1)

问题是函数askForNumber

中的第二个数组
void askForNumber()
{
    int num[5] = { 0, 0, 0, 0, 0 };
    //  ^^
}

只要你在函数内,第二个数组与main中定义的数组共存,但是一离开函数就会再次销毁。用户的值将分配给第二个数组,而第一个数组保持不变。

相反,你需要将main的数组(你传递给calcMagicNumber的数组)进一步传递给askForNumber:

void askForNumber(int num[], size_t size)
{
    // int num[5] = { 0, 0, 0, 0, 0 };
    //     ^^ drop this one!
}

旁注:在C中(这与C ++不同),实际上不接受任何参数的函数被声明/定义为:

void f(void);
//     ^^^^

省略void关键字可以将任意数量的参数传递给函数。试试这个:

void good(void) { }
void bad() { }

int main()
{
    good();
    bad(1);
    bad(1, 2, 3);
    good(1); // <- this is the ONLY line that won't compile...
}

void f()语法是ANSI C之前的剩余时间,其中函数参数在之后声明。不再使用此语法。