2013-10-07 18 views
0

我需要函數drop_balls來返回一個數組,以便我可以讓我的下一個函數使用該數組。我需要它接受一個整數作爲參數,然後返回一個int數組,以便它可以用在我將要做的另一個函數中。我一直在閱讀它只能作爲指針傳遞,但我完全不知道如何編寫代碼,有人可以幫助我。如何讓我的數組返回一個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; 
} 
+0

你到目前爲止的代碼會發生什麼? – canhazbits

+0

使用一個std ::矢量

+1

@mikejones是C++,OP是問關於C – Pankrates

回答

0

這是不完全確定你想要達到的目標,但如果你想給你的功能drop_balls傳遞整數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; 
} 
+0

爲什麼要將int **傳遞給drop_balls,而不是int *?在這種情況下,我不認爲這是必需的。 – dmitri

+0

整個程序應該詢問用戶他們想要放多少球。模擬丟棄那麼多球,並允許它們向左或向右彈跳十次。然後打印陣列,爲每個球落下的每個球顯示一個「o」。 – user2313123

+0

整個程序應該詢問用戶他們想要放多少球。模擬下降很多球,並讓他們反彈十倍要麼就會向左側或右側。然後打印陣列,爲每個球落下的每個球顯示一個「o」。 – user2313123

0

你可以」返回一個在函數中聲明爲局部變量的數組。它位於堆棧上,一旦函數返回,它就不再存在。你將返回數組的地址,這會很糟糕。

相反,你需要在堆上創建磁盤陣列,使用malloc,並返回一個指向它。您需要檢查該指針是否不是NULL,,並且您需要在完成後調用free來釋放內存。您也應該將函數的返回類型更改爲int *

-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. 
} 

我傳遞一個附加的參數,該數組的大小。在不知道大小的情況下,訪問陣列的元素會很危險。

+0

這不會編譯。問題是關於c,而不是C++。 –

+0

@DavidConrad我用malloc替換了新的 – dmitri

+0

Upvoted可以抵消誰低估了這一點。 –

相關問題