在C中有多個參數的特殊庫(stdarg.h)。 有了這個庫,你可以寫函數是這樣的:
int a_function (int x, ...)
{
va_list a_list;
va_start(a_list, x);
}
而且我不覺得有什麼特別的限制。
下面是一個例子,如何使用這個庫: (貸:http://www.cprogramming.com/tutorial/c/lesson17.html)
#include <stdarg.h>
#include <stdio.h>
/* this function will take the number of values to average
followed by all of the numbers to average */
double average (int num, ...)
{
va_list arguments;
double sum = 0;
/* Initializing arguments to store all values after num */
va_start (arguments, num);
/* Sum all the inputs; we still rely on the function caller to tell us how
* many there are */
for (int x = 0; x < num; x++)
{
sum += va_arg (arguments, double);
}
va_end (arguments); // Cleans up the list
return sum/num;
}
int main()
{
/* this computes the average of 13.2, 22.3 and 4.5 (3 indicates the number of values to average) */
printf("%f\n", average (3, 12.2, 22.3, 4.5));
/* here it computes the average of the 5 values 3.3, 2.2, 1.1, 5.5 and 3.3
printf("%f\n", average (5, 3.3, 2.2, 1.1, 5.5, 3.3));
}
據我所知(和你的第一個鏈接說)的限制是C++中的256個參數和c中的127個。行長度沒有問題,因爲您可以將行分成更多行。如果你有巨大的結構作爲參數(不是指向結構體的指針),堆棧大小將只是一個問題。 – mch 2014-09-30 13:37:56
這就是標準規定的「最小」數字(我在我的問題中鏈接了相應的帖子)。我想知道的是「真實」數字,即特定編譯器的侷限性。線條長度:我不確定,但我認爲,預處理器無論如何都會刪除任何換行符(爲什麼我們需要每行結尾處的「;」) - 因此將它們分成多行(我已經我認爲,當然完成)並沒有什麼不同。堆棧大小:好吧,我只需要傳遞指針,這似乎不是一個限制。 – mozzbozz 2014-09-30 14:02:39
它可能是ABI,處理器和編譯器特定的。我想你有一個x86-64。然後*在實踐中*限制與堆棧幀和堆棧大小有關,所以我猜你可能會傳遞幾千個參數。我建議你做一個簡單的測試:編寫一個腳本,爲一些參數N生成一個文件中包含N個參數的函數(例如計算其所有參數的總和),並在另一個文件中調用一個main函數。 – 2014-09-30 14:06:37