开发者

What do I put in the function prototype when using that function with different parameters?

开发者 https://www.devze.com 2023-01-27 08:02 出处:网络
I want to use a function to fill different arrays with data by calling that function thrice. // Function prototype

I want to use a function to fill different arrays with data by calling that function thrice.

// Function prototype

void fill_array();

int main()

{

   int bin_array[15], 

      prb_array[15],

      seq_array[15];

   fill_array(bin_array);

   fill_array(prb_array);

   fill_array(s开发者_如何学Ceq_array);

   return 0;

}

My question is, what parameters should I put up at the function prototype? All three?

// Function prototype
void fill_array(insert parameter here);


In the prototype, you don't even have to put any name at all, just the type:

void fill_array(int[]);

When you define the function, however, you need a name. However, it can be whatever you want:

void fill_array(int joe[]) {
    //...
}

Edit: Although not directly related to the problem at hand, birryree makes an excellent point. You should usually pass the size of an array as well, since otherwise fill_array doesn't know how big the array is:

void fill_array(int[], int);

void do_stuff() {
    int bin_array[15], 

        prb_array[15],

        seq_array[15];

    fill_array(bin_array, sizeof(bin_array) / sizeof(int));
    fill_array(prb_array, sizeof(prb_array) / sizeof(int));
    fill_array(seq_array, sizeof(seq_array) / sizeof(int));
}

void fill_array(int bob[], int length) {
    for(int i = 0; i < length; i++) {
        bob[i] = i * 3;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消