?
?
#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;//定义成float就会失败,因为浮点数默认double类型传递
printf("arg1: %s\n",format);
//====================================
//p = p + sizeof(char *);//这个地方为什么是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);
//========================================注意堆栈也保持4字节对齐
//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;//定义成float就会失败,因为浮点数默认double类型传递
printf("arg1: %s\n",format);
//====================================
p = p + sizeof(char *);//这个地方为什么是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);
//========================================注意堆栈也保持4字节对齐
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;
}
|