真的理解C中的可变参数了吗?

 

#include <stdio.h>

struct mystruct {
	struct mystruct* next;
};

void f(int a, char c, char* s, struct mystruct* b, ...){
	void** varg = (void*)&b;
	varg++;
	while(*s){
		if(*s != '%')
			printf("%c", *s++);
		else {
			s++;
			switch(*s) {
			case '%':
				s++;
				printf("%%");
				break;
			case 's':
				s++;
				printf("%s", *varg++);
				break;
			case 'd':
				s++;
				printf("%d", *varg++);
				break;
			case 'c':
				s++;
				printf("%c", *varg++);
				break;
			default:
				break;
			}
		}	
	}
}

int main(void){
	struct mystruct* b;
	f(2, 'c', "How old are you?\n %d %s %d %c %s", b->next, 3,
	"Hello, ", 4, 'F', "World!\n");
}
发布了159 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_39410618/article/details/89792625