相关疑难解决方法(0)

是否可以编写一个varargs函数将其参数列表发送到另一个varargs函数?

可能重复:
C编程:转发变量参数列表.

我想做的是以printf方式将数据发送到日志库(我无法修改).

所以我想要一个像这样的函数:

void log_DEBUG(const char* fmt, ...) {
   char buff[SOME_PROPER_LENGTH];
   sprintf(buff, fmt, <varargs>);
   log(DEBUG, buff);
}
Run Code Online (Sandbox Code Playgroud)

我可以以某种方式将varargs传递给另一个vararg函数吗?

c++ variadic-functions

25
推荐指数
3
解决办法
2万
查看次数

如何将数组作为参数传递给vararg函数?

我有一些看起来像这样的代码:

uint8_t activities[8];
uint8_t numActivities = 0;
...
activities[numActivities++] = someValue;
...
activities[numActivities++] = someOtherValue;
...
switch (numActivities)
{
   0 : break;
   1 : LogEvent(1, activities[0]);  break;
   2 : LogEvent(1, activities[0], activities[1]);  break;
   3 : LogEvent(1, activities[0], activities[1], activities[2]);  break;
   // and so on
}
Run Code Online (Sandbox Code Playgroud)

其中LogEvent()是一个varargs函数.

有没有更优雅的方式来做到这一点?


[更新] Aplogies @ @ 0x69等.我没有说,有很多情况下LogEvent()无法将数组作为参数.抱歉.

c variadic-functions

9
推荐指数
1
解决办法
2244
查看次数

一个变量args函数如何调用另一个?

假设你有2个功能:

void func(int x,int y,...)
{
 //do stuff
}
void func2(int x,...)
{
 func(x,123,...);
}
Run Code Online (Sandbox Code Playgroud)

你怎么能让这个工作,例如将arg-list传递给另一个函数?

编辑:这是重复,有人可以合并他们或其他什么?

c

6
推荐指数
1
解决办法
1586
查看次数

在C中写入文件和控制台

我正在尝试编写一个允许我写入控制台和C文件的函数.

我有以下代码,但我意识到它不允许我附加参数(如printf).

#include <stdio.h>

int footprint (FILE *outfile, char inarray[]) {
    printf("%s", inarray[]);
    fprintf(outfile, "%s", inarray[]);
}

int main (int argc, char *argv[]) {

    FILE *outfile;
    char *mode = "a+";
    char outputFilename[] = "/tmp/footprint.log";
    outfile = fopen(outputFilename, mode);

    char bigfoot[] = "It Smells!\n";
    int howbad = 10;

    footprint(outfile, "\n--------\n");

    /* then i realized that i can't send the arguments to fn:footprints */
    footprint(outfile, "%s %i",bigfoot, howbad); /* error here! I can't send bigfoot and howbad*/

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我被困在这里 有小费吗?对于我想要发送到函数的参数:footprints,它将包含字符串,字符和整数. …

c printf stdio

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×3

variadic-functions ×2

c++ ×1

printf ×1

stdio ×1