#include
#include
#include
typedef union diff
{
int score;
char pos[20];
}diff_t;
typedef struct person
{
char name[20];
char gender;
char job;
diff_t score_or_pos;
} person_t;
person_t *req_mem(int n);
void input(int n, person_t *parr);
int count(char job, int n, person_t *parr);
void output(int n, person_t *parr);
person_t *free_mem(person_t *parr);
float avg(int n, person_t *parr);
int main(int argc, const char *argv[])
{
int n=3;
//定义指针向堆区的内存
person_t *parr=req_mem(n);
/*//test
person_t arr_p[]={
"zhang", 'f', 's', 98,
"li", 'f', 't', "class teacher",
"wang", 'm', 't', "schoolmaster",
"zhao", 'f', 's', 97
}*/
//输入
input(n, parr);
//计算老师个数
int teachers = count('t', n, parr);
putchar(10);
printf("teacher count=%d\n", teachers);
int students = count('s', n, parr);
putchar(10);
printf("students count=%d\n", students);
//计算学生的平均值
putchar(10);
float av=avg(n, parr);
printf("students avg score=%.2lf\n", av);
//输出
putchar(10);
output(n, parr);
//释放堆区空间
parr=free_mem(parr);
return 0;
}
person_t *req_mem(int n)
{
person_t *p=(person_t*)malloc(sizeof(person_t)*n);
if(NULL==p)
{
printf("error");
return NULL;
}
return p;
}
void input(int n, person_t *parr)
{
for(int i=0; iname);
printf("please input %d gender:", i+1);
scanf(" %c", &(parr+i)->gender);
printf("please input %d job:", i+1);
scanf(" %c", &(parr+i)->job);
if('s'==((parr+i)->job) || 'S'==(parr+i)->job)
{
printf("please input %d score:", i+1);
scanf("%d", &(parr+i)->score_or_pos.score);
}
else if('t'==((parr+i)->job) || 'T'==(parr+i)->job)
{
printf("please input %d position:", i+1);
scanf("%s", (parr+i)->score_or_pos.pos);
}
}
}
int count(char job, int n, person_t *parr)
{
int count_s=0;
int count_t=0;
for(int i=0; ijob;
if(cj=='t'||cj=='T')
{
count_t++;
}
else if(cj=='s'||cj=='S')
{
count_s++;
}
}
if (job=='t'||job=='T')
{
return count_t;
}
else if('s'==job||'T'==job)
{
return count_s;
}
return -1;
}
float avg(int n, person_t *parr)
{
int sum=0;
int count=0;
for(int i=0; ijob;
if('s'==cj||'S'==cj)
{
sum+=(parr+i)->score_or_pos.score;
count++;
}
}
if(count>0){
return (float)sum*1.0/count;
}
else
{
return 0;
}
}
void output(int n, person_t *parr)
{
for(int i=0; iname, (parr+i)->gender, (parr+i)->job );
char cj=(parr+i)->job;
if('s'==cj||'S'==cj)
{
printf("score=%d\n", (parr+i)->score_or_pos.score);
}
else if('t'==cj||'T'==cj)
{
printf("position=%s\n", (parr+i)->score_or_pos.pos);
}
putchar(10);
}
putchar(10);
}
person_t *free_mem(person_t *parr)
{
if(NULL==parr)
{
return NULL;
}
free(parr);
parr=NULL;
return parr;
}
please input 1 name:w
please input 1 gender:f
please input 1 job:s
please input 1 score:90
please input 2 name:a
please input 2 gender:f
please input 2 job:s
please input 2 score:92
please input 3 name:n
please input 3 gender:f
please input 3 job:t
please input 3 position:class teacher
teacher count=1
students count=2
students avg score=91.00
No. 1 person info:
name=w, gender=f, job=s, score=90
No. 2 person info:
name=a, gender=f, job=s, score=92
No. 3 person info:
name=n, gender=f, job=t, position=class
struct s1
{
char ch;
int a;
double b;
char c1;
};
s1-->20 | |
0x00 | ch |
0x01 | |
0x02 | |
0x03 | |
0x04 | a |
0x05 | |
0x06 | |
0x07 | |
0x08 | b |
0x09 | |
0x0A | |
0x0B | |
0x0C | |
0x0D | |
0x0E | |
0x0F | |
0x20 | c1 |
0x21 | |
0x22 | |
0x23 |
struct s2
{
char ch;
int a;
double b;
};
s2-->16 | |
0x00 | ch |
0x01 | |
0x02 | |
0x03 | |
0x04 | a |
0x05 | |
0x06 | |
0x07 | |
0x08 | b |
0x09 | |
0x0A | |
0x0B | |
0x0C | |
0x0D | |
0x0E | |
0x0F |
struct s3
{
char i;
struct
{
char c;
};
char a;
char b;
char d;
char e;
char f;
};
s3->12 | |
0x00 | i |
0x01 | |
0x02 | |
0x03 | |
0x04 | c |
0x05 | a |
0x06 | b |
0x07 | d |
0x08 | e |
0x09 | f |
0x0A | |
0x0B |
#include
#include
#include
#define EXCHANGE a=a+b; b=a-b; a=a-b;
int main(int argc, const char *argv[])
{
int a=2;
int b=3;
printf("before exchange: a=%d, b=%d\n", a, b );
EXCHANGE;
printf("afterbefore exchange: a=%d, b=%d\n", a, b );
return 0;
}
1.static修饰全局变量未初始化,默认结果为0
2.static修饰的局部,延长生命周期至本文件有效,但不是作用域
3.static修饰函数, 不可以跨文件调用,static修饰的函数生命周期在本文件有效
4.static修饰指针, static修饰的指针不要指向auto类型的变量地址