struct and union example
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);
}
struct and union example的更多相关文章
- 关于C中struct和union长度的详解
这几天看<代码大全>中的第十三章---不常见的数据类型,里面讲解到了C语言中的struct以及对指针的解释,联想到以前看过相关的关于C语言中stuct长度的文章,只是现在有些淡忘了,因此今 ...
- 【转】C/C++ struct/class/union内存对齐
原文链接:http://www.cnblogs.com/Miranda-lym/p/5197805.html struct/class/union内存对齐原则有四个: 1).数据成员对齐规则:结构(s ...
- 【转】结构struct 联合Union和枚举Enum的细节讨论
结构struct 联合Union和枚举Enum的细节讨论 联合(Union)是一种构造数据类型,它提供了一种使不同类型数据类型成员之间共享存储空间的方法,同时可以实现不同类型数据成员之间的自动类型转换 ...
- struct 和union的区别
union ( 共用体):构造数据类型,也叫联合体 用途:使几个不同类型的变量共占一段内存(相互覆盖) struct ( 结构体 ):是一种构造类型 用途: 把不同的数据组合成一个整体——自定义数据 ...
- C语言进阶——struct和union分析10
struct的小秘密: C语言中的struct可以看作变量的集合 struct的问题:空结构体占用多大内存呢? 程序实例1: #include <stdio.h> struct TS { ...
- <转> Struct 和 Union区别 以及 对内存对齐方式的说明
转载地址:http://blog.csdn.net/firefly_2002/article/details/7954458 一.Struct 和 Union有下列区别: 1.在存储多个成员信息时,编 ...
- struct与 union的基本用法
结构体与联合体是C语言的常见数据类型,可对C的基本数据类型进行组合使之能表示复杂的数据结构,意义深远,是优异代码的必备工具.一. struct与 union的基本用法,在语法上union ...
- struct和union
struct的小秘密 C语言中的struct可以看做变量的集合,struct的问题: 空结构体占用多大内存? 例子1:空结构体的大小 #include<stdio.h> struct ST ...
- c++ struct enum union加typedef与不加typedef
struct/enum/union加typedef与不加typedef 匿名结构体 struct { int a; int b; } v; // 这里表示定义了一个结构体的变量v,且结构体类型没有名字 ...
- struct与union字节大小的终极解释
1.字节对齐的细节和编译器实现相关,但一般而言,如在windows下,就VC而言,满足一下三个准则:1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除:2) 结构体每个成员相对于结构体首地址 ...
随机推荐
- Python每日一题 003
将 002 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中. 代码 import pymysql import uuid def get_id(): for i in ra ...
- 【已转移】【缓存与性能优化】一篇文章搞掂:Redis
本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 一.什么是Redis 全称: Remote Dictionary Server 远程字典服务器 实质: 一个缓存结构服务器或数据结 ...
- windows2003 disk mirror failed redundency
操作前請確認 mirror 磁盤已備份 windows2003 disk mirror failed redundency時,offline + online並不可以自動修復. 需要offline f ...
- 《ArcGIS Runtime SDK for .NET开发笔记》--三维功能
介绍 在ArcGIS Runtim SDK for .NET 10.2.6中,新添加了三维地图功能.在ArcGIS中,我们将三维地图称为Scene(场景),所以在Runtime SDK SDK for ...
- oraToolKit Oracle安装辅助工具的使用方法
目录 目录 otk使用方式 使用oraToolKit进行检测安装包情况 使用oraToolKit进行检测操作系统情况 最后 otk使用方式 oraToolkit的安装在RHEL6.1 安装 Oracl ...
- PHP面试 javascript和jQuery 基础
JavaScript基础 JavaScript语法 变量的定义:变量必须以字母开头 可以使用$和 _ 符号开头 变量名称对大小写敏感 使用 var 关键词声明变量 可以一次声明多个变量 ...
- Encode
by kinsly 本文的内容均基于python3.5 编码一直是python中的大坑,反正我是一直没搞明白,今天在做爬虫的时候,觉得实在是有必要把这些东西整理一下. 什么是编码 简单的来说就是,为了 ...
- CTU OPEN 2017 Punching Power /// 最大独立集
题目大意: 给定n 给定n个机器的位置 要求任意两个机器间的距离至少为1.3米 求最多能选择多少个机器 至少为1.3米 说明若是位于上下左右一步的得放就不行 将机器编号 将不能同时存在的机器连边 此时 ...
- Java中使用File类删除文件夹和文件
删除工具类: import java.io.File; public class DeleteAll{ public static void deleteAll(File file){ if(file ...
- Stm32CubeMX5 配置 STM32的串口DMA接受方式 --- 基于 stm32f051k8u6
实现的功能: 使用MDA方式把串口接受的数据在发送给串口(当然也可以做其他解析控制使用) 1. 先初始化 时钟使用外部的晶振配置系统时钟为48Mhz 2. 串口参数配置 3. 使能中断 4. 配置串 ...