0

次の関数でその配列を使用できるようにするには、配列を返す関数 drop_balls が必要です。整数をパラメータとして取り、後で作成する別の関数で使用できるように int 配列を返す必要があります。ポインターとしてのみ渡すことができると読んでいますが、それがどのようにコーディングされるかまったくわかりません。誰か助けてください。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


/* Prototype for drop_Balls, int parameter is number of balls being dropped */
int [] drop_balls(int); 

/* Gets the number of balls that need to be dropped by the user */
int get_num_balls(); 


int main()
{
    drop_balls(get_num_balls());
}


int get_num_balls()
{
    int num_balls;
    printf("How many balls should be dropped? ");
    scanf("%d", &num_balls);
    /* Ensure that it is atleast one ball */
    while(num_balls <= 0) 
    {
        printf("You have to drop at least one ball! \n ");
        printf("How many balls should be dropped? ");
        scanf("%d", &num_balls);
    }
    /* Return the number of balls that will be dropped */
    return num_balls; 
}



int [] drop_balls(int num_balls)
{
    /* Keeps track of how many balls landed where */
    int ball_count[21]; 
    /* What number ball are we on */
    int ball_num = 0; 
    /* Seed the generator */
    srand(time(NULL)); 
    /* Do the correct # of balls */
    for(ball_num = 0; ball_num < num_balls; ball_num++ ) 
    {
  /* Start at 10 since its the middle of 21 */
        int starting_point = 10; 
    /* Bounce each ball 10 times */
        for(starting_point = 10; starting_point > 0; starting_point--) 
        {
           int number;
       /* Create a random integer between 1-100 */
           number = rand() % 100; 
       /* If its less than 50 bounce to the right once */
           if(number >= 50) 
           {
               starting_point++;
           }
       /* If its greater than 50, bounce to the left once */
           else 
           {
               starting_point--;
           }
        }
    /* Add one to simulate one ball landing there */
        ball_count[starting_point]++;  
    }
    return ball_count;
}
4

3 に答える 3

0

達成したいことは完全にはわかりませんが、balls関数に整数の配列を渡したい場合は、次のdrop_ballsように宣言し ます。

void drop_balls(int **balls);

int main ()
{
    int *balls; /* This is your array of balls*/

    /* Allocate memory for `number_of_balls` number of balls */
    balls = malloc(number_of_balls * sizeof(*balls));
    if (balls == NULL)
         printf("Error: failed to allocate memory");

    /* call the function by passing the pointer to your array of balls */
    drop_balls(&balls);

    free(balls);
    return 0;
}
于 2013-10-07T02:06:17.353 に答える
0

関数でローカル変数として宣言された配列を返すことはできません。スタック上に存在し、関数が戻ると存在しなくなります。配列があった場所のアドレスを返すことになりますが、これは悪いことです。

代わりに、ヒープ上に配列を作成し、malloc,それへのポインターを使用して返す必要があります。このポインターがそうでないことを確認する必要がNULL,あります。また、使い終わったら、呼び出しfreeてメモリーを解放する必要があります。関数の戻り値の型int *も同様に変更する必要があります。

于 2013-10-07T02:09:09.020 に答える
-1

配列を返す関数の例はmalloc()、標準ライブラリ関数です。配列を割り当て、それで何かを行い、それを呼び出し元に返す独自の関数を書くことができます:

int *function_returning_an_array_of_size_n(int n)
{
   int *array = (int*)malloc(n * sizeof(int));
   // do something useful with the array
   // ...
   return array;
}

配列をパラメーターとして受け取る関数の例は、これfree()も標準ライブラリ関数です。

配列をパラメーターとして受け取る関数を作成する方法を次に示します。

void function_receiving_an_array_and_size_n(int *array, int n)
{
   // Do something with array. If accessing elements beware the array size violation.
}

追加のパラメーターである配列サイズを渡しています。サイズを知らずに配列の要素にアクセスするのは危険です。

于 2013-10-07T02:12:40.323 に答える