当前位置:网站首页>Analysis and implementation of variable parameters in C language

Analysis and implementation of variable parameters in C language

2022-06-13 09:39:00 LuckyDog0623

 

 

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

struct person{
	char *name;
	int age;
	char score;
};
struct person1{
	char *name;
	int age;
	char score;
}__attribute__((packed));
struct person2{
	char *name;
	int age;
	char score;
}__attribute__((aligned(4)));


/* int printf(const char *format, ...); */
int push_test(const char *format,...)
{
	//char *p=(char *)&format;
	va_list p;
	int i;
	struct person per;
	char c;
	double d;// Defined as float Will fail , Because floating point numbers default to double Type passing 
	
	printf("arg1: %s\n",format);
	//====================================
	//p = p + sizeof(char *);// Why is this place char*
	va_start(p,format);
	
	//i=*((int *)p);
	i=va_arg(p,int);
	printf("arg2:%d\n",i);
	
	//=====================================
	//p=p+sizeof(int);
	//per=*((struct person*)p);
	per=va_arg(p,struct person);
	printf("arg3:.name=%s,.age=%d,.score=%c\n",per.name,per.age,per.score);
	
	//=======================================
	//p=p+sizeof(struct person);
	//c=*((char *)p);
	c=va_arg(p,int);
	printf("arg4:%c\n",c);
	
	//======================================== Note that the stack also holds 4 Byte alignment 
	//p=p+(sizeof(char) +3);// p=p+((sizeof(char) +3)& ~3);
	//d=*((double *)p);
	d=va_arg(p,double);
	va_end(p);
	printf("arg5:%f\n",d);
	
	return 0;
}
int main(int arhgc, char **argv)
{
	struct person per={"tangjun",100,'B'};
	
	printf("sizeof(char          )=%d\n", sizeof(char	));
	printf("sizeof(int           )=%d\n", sizeof(int		));
	printf("sizeof(char*         )=%d\n", sizeof(char *	));
	printf("sizeof(char**        )=%d\n", sizeof(char**	));
	printf("sizeof(struct person )=%d\n", sizeof(struct person));
	printf("sizeof(struct person1)=%d\n", sizeof(struct person1));
	printf("sizeof(struct person2)=%d\n", sizeof(struct person2));
	push_test("abcd",123, per,'c',2.79);
	return 0;
}
#include <stdio.h>

struct person{
	char *name;
	int age;
	char score;
};
struct person1{
	char *name;
	int age;
	char score;
}__attribute__((packed));
struct person2{
	char *name;
	int age;
	char score;
}__attribute__((aligned(4)));


/* int printf(const char *format, ...); */
int push_test(const char *format,...)
{
	char *p=(char *)&format;
	int i;
	struct person per;
	char c;
	double d;// Defined as float Will fail , Because floating point numbers default to double Type passing 
	
	printf("arg1: %s\n",format);
	//====================================
	p = p + sizeof(char *);// Why is this place char*
	i=*((int *)p);
	printf("arg2:%d\n",i);
	//=====================================
	p=p+sizeof(int);
	per=*((struct person*)p);
	printf("arg3:.name=%s,.age=%d,.score=%c\n",per.name,per.age,per.score);
	
	//=======================================
	p=p+sizeof(struct person);
	c=*((char *)p);
	printf("arg4:%c\n",c);
	
	//======================================== Note that the stack also holds 4 Byte alignment 
	p=p+(sizeof(char) +3);// p=p+((sizeof(char) +3)& ~3);
	
	d=*((double *)p);
	printf("arg5:%f\n",d);
	
	return 0;
}
int main(int arhgc, char **argv)
{
	struct person per={"tangjun",100,'B'};
	
	printf("sizeof(char          )=%d\n", sizeof(char	));
	printf("sizeof(int           )=%d\n", sizeof(int		));
	printf("sizeof(char*         )=%d\n", sizeof(char *	));
	printf("sizeof(char**        )=%d\n", sizeof(char**	));
	printf("sizeof(struct person )=%d\n", sizeof(struct person));
	printf("sizeof(struct person1)=%d\n", sizeof(struct person1));
	printf("sizeof(struct person2)=%d\n", sizeof(struct person2));
	push_test("abcd",123, per,'c',2.79);
	return 0;
}

原网站

版权声明
本文为[LuckyDog0623]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270530115560.html