以指针为参数的可变参数函数

Variadic function with pointers as parameters

程序应打印给定数组(大小为 8)的前三个、五个和八个元素的总和。 我已经能够像这样在 main 函数中传递指针:

代码:

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
int  sum (int count,int data[],...) {
    int res=0;
    for(int i=0;i<count;i++)
    {
        res=res+data[i];   
    }
    return res;
}
int main(){
    int elements[8]={11,2,33,5,8,48,2,-8};
    int three_elements;
    int five_elements;
    int eight_elements;
    three_elements=sum(3, &elements[0], &elements[1], &elements[2]);
    five_elements=sum(5, &elements[0], &elements[1], &elements[2], &elements[3], &elements[4]);
    eight_elements=sum(8, &elements[0], &elements[1], &elements[2], &elements[3], &elements[4],&elements[5], &elements[6], &elements[7]);
    printf("First Three elements sum:%d\n",three_elements);
    printf("First five elements sum:%d\n",five_elements);
    printf("First eight elements sum:%d",eight_elements);
    return 0;
}

OUTPUT:

First Three elements sum:46
First five elements sum:59
First eight elements sum:101

如何将参数直接传递给 sum 函数?我试着这样做,但编译器一直给我错误:

代码:

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
int  sum (int count,int* data[],...) {
    int res=0;
    for(int i=0;i<count;i++)
    {
        res=res+data[i];   
    }
    return res;
}
int main(){
    int elements[8]={11,2,33,5,8,48,2,-8};
    int three_elements;
    int five_elements;
    int eight_elements;
    three_elements=sum(3, elements[0], elements[1], elements[2]);
    five_elements=sum(5, elements[0], elements[1], elements[2], elements[3], elements[4]);
    eight_elements=sum(8, elements[0], elements[1], elements[2], elements[3], elements[4],elements[5], elements[6], elements[7]);
    printf("First Three elements sum:%d\n",three_elements);
    printf("First five elements sum:%d\n",five_elements);
    printf("First eight elements sum:%d",eight_elements);
    return 0;
}

可变函数的总和

看看man:

The va_start() macro initializes ap for subsequent use by va_arg() and va_end(), and must be called first.

The va_arg() macro expands to an expression that has the type and value of the next argument in the call. The argument ap is the va_list ap initialized by va_start(). Each call to va_arg() modifies ap so that the next call returns the next argument. The argument type is a type name specified so that the type of a pointer to an object that has the specified type can be obtained simply by adding a * to type.

如果没有指针,您可以通过以下方式进行:

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

int  sum (int len, int value, ...) {
    va_list args; // initialize va_list

    int res = value;
    va_start(args, value); // start va
    for (int i = 1; i < len; i++) // iterate over other arguments
        res += va_arg(args, int); // add the next argument to res
    va_end(args); // don't forget to end va
    return res;
}
int main(){
    int elements[8]={11,2,33,5,8,48,2,-8};
    int three_elements;
    int five_elements;
    int eight_elements;
    three_elements=sum(3, elements[0], elements[1], elements[2]);
    five_elements=sum(5, elements[0], elements[1], elements[2], elements[3], elements[4]);
    eight_elements=sum(8, elements[0], elements[1], elements[2], elements[3], elements[4],elements[5], elements[6], elements[7]);
    printf("First Three elements sum:%d\n",three_elements);
    printf("First five elements sum:%d\n",five_elements);
    printf("First eight elements sum:%d\n",eight_elements);
    return 0;
}

为了通过指针得到总和,你可以这样做:

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

int  sum (int len, int *value, ...) {
    va_list args; // initialize va_list

    int res = *value;
    va_start(args, *value); // start va
    for (int i = 1; i < len; i++) // iterate over other arguments
        res += (int)*va_arg(args, int*); // add the next argument to res
    va_end(args); // don't forget to end va
    return res;
}
int main(){
    int elements[8]={11,2,33,5,8,48,2,-8};
    int three_elements;
    int five_elements;
    int eight_elements;
    three_elements=sum(3, &elements[0], &elements[1], &elements[2]);
    five_elements=sum(5, &elements[0], &elements[1], &elements[2], &elements[3], &elements[4]);
    eight_elements=sum(8, &elements[0], &elements[1], &elements[2], &elements[3], &elements[4], \
        &elements[5], &elements[6], &elements[7]);
    printf("First Three elements sum:%d\n",three_elements);
    printf("First five elements sum:%d\n",five_elements);
    printf("First eight elements sum:%d\n",eight_elements);
    return 0;
}

输出:

First Three elements sum:46
First five elements sum:59
First eight elements sum:101