4

我的问题vsprintf是我无法直接获取输入参数,我必须先一个一个地获取输入并将它们保存在void**,然后将其传递void**vsprintf(),这对windows来说很好,但是当我来到64位linux时,gcc无法编译因为它不允许从 to 转换void**va_list有没有人可以给我一些帮助,我应该在 linux 下怎么做?

我的部分代码是:

void getInputArgs(char* str, char* format, ...)
{
    va_list args;
    va_start(args, format);
    vsprintf(str, format, args);
    va_end(args);
}  

void process(void)
{
    char s[256];
    double tempValue;
    char * tempString = NULL;
    void ** args_ptr =NULL;
    ArgFormatType format;   //defined in the lib I used in the code
    int numOfArgs = GetNumInputArgs();  // library func used in my code

    if(numOfArgs>1)
    {
        args_ptr = (void**) malloc(sizeof(char)*(numOfArgs-1));
        for(i=2; i<numOfArgs; i++)
        {
            format = GetArgType();    //library funcs

            switch(format)
            {
                case ArgType_double:
                    CopyInDoubleArg(i, TRUE, &tempValue);   //lib func
                    args_ptr[i-2] = (void*) (int)tempValue;    
                    break;

                case ArgType_char:
                    args_ptr[i-2]=NULL;
                    AllocInCharArg(i, TRUE, &tempString);  //lib func
                    args_ptr[i-2]= tempString;
                break;
            }
        }
    }

    getInputArgs(s, formatString, (va_list) args_ptr);   /////Here is the location where gcc cannot compile 
}

非常感谢!!

4

1 回答 1

1

The problem is, your function gets ..., but you are passing it a va_list. ... is used for a usage like this:

 getInputArgs(s, formatString, arg1, arg2, arg3, arg4 /* etc */);

and it won't work with va_list. Unfortunately, there is not an easy way to create a va_list from different parameters instead of getting it from .... See this question for example.

What you should do is to change the way you want to print to the string.

You can have:

char s[256];
int so_far = 0;

And in your for loop instead of something like this:

CopyInDoubleArg(i, TRUE, &tempValue);   //lib func
args_ptr[i-2] = (void*) (int)tempValue;

You write:

CopyInDoubleArg(i, TRUE, &tempValue);   //lib func
if (so_far < 256)  /* 256 is the maximum length of s */
    so_far += snprintf(s + so_far, 256 - so_far, "%lf", tempValue);

Something along these lines. This way, you create the string one by one, appending each element to the previous, instead of trying to make it all at once.

于 2012-07-27T16:00:38.410 回答