开发者

C variadic functions how to get string from input

I want to get a string (const char*) from my variadic function input values. But this code b开发者_运维百科elow doesn't work..... A runtime error is the result

void print(const char fmt[], ...) {
    va_list ap;
    const char *p=fmt;
    va_start(ap,fmt);
    while(*p) {
        if(*p == '%') {
            p ++;
            if (*p == 'i') {
                int num = va_arg(ap, int);
                fprintf(output, "%d", num);
            } else if (*p == 'f') {
                float num = va_arg(ap, float);
                fprintf(output, "%f", num);
            } else if (*p == 's') {
                const char* str = va_arg(ap, const char*);
                fprintf(output, "%s", str);
            } else
                break;
            p ++;
        } else
            break;
    }
    va_end(ap);
}
// This is how I call the function:
print("%s%f", "Num: ", 12.34);

Any ideas?


It's undoubtedly in some part of the code you haven't shown [edit: or hadn't shown at the time]. Here's a (thoroughly incomplete) imitation of printf (and family) I wrote years ago. It doesn't implement the usual width/precision/flags/etc., but works for simple things like %d, %s, etc.

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

int my_vfprintf(FILE *file, char const *fmt, va_list arg) {

    int int_temp;
    char char_temp;
    char *string_temp;
    char ch;
    int length = 0;

    char buffer[512];

    while ( ch = *fmt++) {
        if ( '%' == ch ) {
            switch (ch = *fmt++) {
                /* %% - print out a single %    */
                case '%':
                    fputc('%', file);
                    length++;
                    break;

                /* %c: print out a character    */
                case 'c':
                    char_temp = va_arg(arg, int);
                    fputc(char_temp, file);
                    length++;
                    break;

                /* %s: print out a string       */
                case 's':
                    string_temp = va_arg(arg, char *);
                    fputs(string_temp, file);
                    length += strlen(string_temp);
                    break;

                /* %d: print out an int         */
                case 'd':
                    int_temp = va_arg(arg, int);
                    itoa(int_temp, buffer, 10);
                    fputs(buffer, file);
                    length += strlen(buffer);
                    break;

                /* %x: print out an int in hex  */
                case 'x':
                    int_temp = va_arg(arg, int);
                    itoa(int_temp, buffer, 16);
                    fputs(buffer, file);
                    length += strlen(buffer);
                    break;
            }
        }
        else {
            putc(ch, file);
            length++;
        }
    }
    return length;
}

int my_printf(char const *fmt, ...) {
    va_list arg;
    int length;

    va_start(arg, fmt);
    length = my_vfprintf(stdout, fmt, arg);
    va_end(arg);
    return length;
}

int my_fprintf(FILE *file, char const *fmt, ...) {
    va_list arg;
    int length;

    va_start(arg, fmt);
    length = my_vfprintf(file, fmt, arg);
    va_end(arg);
    return length;
}


#ifdef TEST 

int main() {
    my_printf("%s", "Some string");
    return 0;
}

#endif
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜