C 如何传递数组';s作为函数的总元素';s位置参数?

C 如何传递数组';s作为函数的总元素';s位置参数?,c,C,我在c中有一个数组: int array[] = {1, 2, 3, 4}; 我有一个函数的原型如下: int add_all(int a, int b, int c, int d); array=[1,2,3,4] add_all(*array) 但是如何一次传递数组的所有元素作为函数的参数呢 我知道在python中,可以这样做: int add_all(int a, int b, int c, int d); array=[1,2,3,4] add_all(*array) 有人能告

我在c中有一个数组:

int array[] = {1, 2, 3, 4};
我有一个函数的原型如下:

int add_all(int a, int b, int c, int d);
array=[1,2,3,4]
add_all(*array)
但是如何一次传递数组的所有元素作为函数的参数呢

我知道在python中,可以这样做:

int add_all(int a, int b, int c, int d);
array=[1,2,3,4]
add_all(*array)
有人能告诉我如何在C语言中达到同样的效果吗?

使用这个:

int add_all(array, 4);
当你调用一个函数,并试图给它传递一个数组,这个数组 将转换为指向数组第一个元素的指针

功能原型

int add_all(int[] , int size);

使用以下命令:

int add_all(array, 4);
当你调用一个函数,并试图给它传递一个数组,这个数组 将转换为指向数组第一个元素的指针

功能原型

int add_all(int[] , int size);

试试这个:

#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/control/while.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/tuple/push_back.hpp>
#include <boost/preprocessor/tuple/reverse.hpp>

#define ARRAY_ELEM(x,n) x[n]

#define PRED(n, state) BOOST_PP_TUPLE_ELEM(3, 1, state)

#define OP(d, state) \
   OP_D( \
      d, \
      BOOST_PP_TUPLE_ELEM(3, 0, state), \
      BOOST_PP_TUPLE_ELEM(3, 1, state), \
      BOOST_PP_TUPLE_ELEM(3, 2, state) \
   ) 

#define OP_D(d, res, c, arr_name) \
   ( \
      BOOST_PP_TUPLE_PUSH_BACK(res,ARRAY_ELEM(arr_name,BOOST_PP_DEC(c))), \
      BOOST_PP_DEC(c), \
      arr_name \
   ) \


#define UNPACK_REVERSE(ARR_NAME,N) \
    BOOST_PP_TUPLE_ELEM( \
      3, 0, \
      BOOST_PP_WHILE(PRED, OP, ((ARRAY_ELEM(ARR_NAME,BOOST_PP_DEC(N))), \
                    BOOST_PP_DEC(N),ARR_NAME) ) \
   )
/*The macro to be called*/
#define UNPACK(ARR_NAME,N) \
    BOOST_PP_TUPLE_REVERSE(UNPACK_REVERSE(ARR_NAME,N))
此宏在循环中使用Boost的预处理器库来生成和扩展元组中的数组元素。希望有帮助。

试试这个:

#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/control/while.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/tuple/push_back.hpp>
#include <boost/preprocessor/tuple/reverse.hpp>

#define ARRAY_ELEM(x,n) x[n]

#define PRED(n, state) BOOST_PP_TUPLE_ELEM(3, 1, state)

#define OP(d, state) \
   OP_D( \
      d, \
      BOOST_PP_TUPLE_ELEM(3, 0, state), \
      BOOST_PP_TUPLE_ELEM(3, 1, state), \
      BOOST_PP_TUPLE_ELEM(3, 2, state) \
   ) 

#define OP_D(d, res, c, arr_name) \
   ( \
      BOOST_PP_TUPLE_PUSH_BACK(res,ARRAY_ELEM(arr_name,BOOST_PP_DEC(c))), \
      BOOST_PP_DEC(c), \
      arr_name \
   ) \


#define UNPACK_REVERSE(ARR_NAME,N) \
    BOOST_PP_TUPLE_ELEM( \
      3, 0, \
      BOOST_PP_WHILE(PRED, OP, ((ARRAY_ELEM(ARR_NAME,BOOST_PP_DEC(N))), \
                    BOOST_PP_DEC(N),ARR_NAME) ) \
   )
/*The macro to be called*/
#define UNPACK(ARR_NAME,N) \
    BOOST_PP_TUPLE_REVERSE(UNPACK_REVERSE(ARR_NAME,N))


此宏在循环中使用Boost的预处理器库来生成和扩展元组中的数组元素。希望有帮助。

谢谢。我知道你说的,但我不想改变我的功能。所以在传递参数的过程中我需要一些神奇的方法。@RyanLi没有神奇的方法,你必须传递4个参数。您可以更改函数或编写函数包装器。@2501谢谢。也许函数包装器确实是我所需要的。那么,你能告诉我如何根据我的问题编写包装器吗。我将非常感谢。@RyanLi函数包装器只是一个普通函数,它接受参数并调用您首先要调用的函数。@2501好的,我知道您所说的。但是我陷入了一个问题,那就是如何将参数传递给我想要调用的函数。 我会写包装纸的字体。         int wrapper(int(*fun)(int,int,int,int,),int*数组,int大小){/*如何传递参数?*/}谢谢。我知道你说的,但我不想改变我的功能。所以在传递参数的过程中我需要一些神奇的方法。@RyanLi没有神奇的方法,你必须传递4个参数。您可以更改函数或编写函数包装器。@2501谢谢。也许函数包装器确实是我所需要的。那么,你能告诉我如何根据我的问题编写包装器吗。我将非常感谢。@RyanLi函数包装器只是一个普通函数,它接受参数并调用您首先要调用的函数。@2501好的,我知道您所说的。但是我陷入了一个问题,那就是如何将参数传递给我想要调用的函数。 我会写包装纸的字体。         int wrapper(int(*fun)(int,int,int,int,),int*数组,int大小){/*如何传递参数?*/}很好的方法:+1非常好,谢谢。但是我在使用C语言,所以我不熟悉C++中的Boost库。@ RynLee:这是C而不是C++。好方法:+1非常好,谢谢。但是我在使用C语言,所以我不熟悉C++中的Boost库。@ RynLi:这是C不是C++ + .AHMEM.FRARAG,它添加了与问题不相关的标签,因此你可以发布一个依赖于该标签的答案是不正确的。@ 2501:好的。明白了。@ahmed.Farrag添加与问题无关的标签,这样你就可以发布依赖于该标签的答案。@2501:ok。知道了。