struct and union example
2013-07-07 12:00
330 查看
1. StructHandler.c:
/*
* StructHandler.c
*
* Created on: Jul 6, 2013
* Author: wangle
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void modifyName(struct student *p);
int main(){
struct student{
char name[50];
char dep[50];
long no;
float score[4];
};
typedef struct student stu_t;
struct student stu[50]={
"wangle", "Math", 80,80,90.5,99,100,
"xuyehui", "biological", 90,90,90,70,100,
"mengmeng", "Math", 100,100,100,100,90
};
int i;
for(i=0; i<3; i++){
printf("%s, %s, %ld, %.2f,%.2f,%.2f,%.2f\n", stu[i].name, stu[i].dep, stu[i].no,
stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3]);
}
stu_t * p = stu;
puts((*(p+1)).name); //(1) a pointer call style.
puts((p+1)->name); //(2) common pointer call style. (1) and (2) is the same.
printf("%s\n", (p+2)->name);
printf("%s\n", (*(p+2)).name);
printf("no = %ld\n", p->no);
void modifyName(struct student * p){
p->no = 123456;
}
modifyName(p);
printf("%s\n", (p)->name);
printf("no = %ld\n", p->no);
}
2. UnionHandler.c
/*
* UnionHandler.c
*
* Created on: Jul 6, 2013
* Author: wangle
*/
#include <stdio.h>
int main(){
union unidate{
char c;
int i;
long l;
float f
};
union unidate x;
x.c=65;
printf("c=%c\n", x.c);
x.i = 10;
printf("i=%d\n",x.i);
x.l = 100;
printf("l=%ld\n", x.l);
x.f = 90.5;
printf("f=%.1f\n", x.f);
printf("c=%c\n", x.c);
}
/*
* StructHandler.c
*
* Created on: Jul 6, 2013
* Author: wangle
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void modifyName(struct student *p);
int main(){
struct student{
char name[50];
char dep[50];
long no;
float score[4];
};
typedef struct student stu_t;
struct student stu[50]={
"wangle", "Math", 80,80,90.5,99,100,
"xuyehui", "biological", 90,90,90,70,100,
"mengmeng", "Math", 100,100,100,100,90
};
int i;
for(i=0; i<3; i++){
printf("%s, %s, %ld, %.2f,%.2f,%.2f,%.2f\n", stu[i].name, stu[i].dep, stu[i].no,
stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].score[3]);
}
stu_t * p = stu;
puts((*(p+1)).name); //(1) a pointer call style.
puts((p+1)->name); //(2) common pointer call style. (1) and (2) is the same.
printf("%s\n", (p+2)->name);
printf("%s\n", (*(p+2)).name);
printf("no = %ld\n", p->no);
void modifyName(struct student * p){
p->no = 123456;
}
modifyName(p);
printf("%s\n", (p)->name);
printf("no = %ld\n", p->no);
}
2. UnionHandler.c
/*
* UnionHandler.c
*
* Created on: Jul 6, 2013
* Author: wangle
*/
#include <stdio.h>
int main(){
union unidate{
char c;
int i;
long l;
float f
};
union unidate x;
x.c=65;
printf("c=%c\n", x.c);
x.i = 10;
printf("i=%d\n",x.i);
x.l = 100;
printf("l=%ld\n", x.l);
x.f = 90.5;
printf("f=%.1f\n", x.f);
printf("c=%c\n", x.c);
}
相关文章推荐
- struct and union 的区别
- struct、union、enum and sizeof
- struct union and endian
- 关于 Union,Struct and Class的大小计算问题的思考(转)&union和struct类型的大小计算(转)
- union and struct
- 结构体struct and 联合体 union
- struct、union、enum and sizeof
- struct and union
- 关于Union,Struct and Class的大小计算问题
- struct位域,对齐以及union作为class需要注意的地方
- Spring(6)-Spring Collections (List, Set, Map, and Properties) example
- 联合、结构和类(Union, Struct, Class)
- struct, union
- union和struct类型大小的跨平台讨论
- union 和 struct 的区别与联系
- struct与union的对齐方式解析
- Difference Between String , StringBuilder And StringBuffer Classes With Example : Java
- C语言深度解剖——读书笔记-3、关键字(const、 volatile、extern、struct、union、enum、typedef)
- JAVA 与C语言中struct union 字节流转换方案 Javolution
- AndEngine Example(4):SpriteRemoveExample