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) 结构体每个成员相对于结构体首地址 ...
随机推荐
- 【dart学习】-- Dart之网络请求操作
Flutter的请求网络有多种方式,一种是使用dart io中的HttpClient发起的请求,一种是使用dio库,另一种是使用http库,先学一下get和post,put.delete就等后面用到在 ...
- Java中如何修改Jar中的内容
一.摘要 好长时间没写blog了,之前换了一家公司.表示工作更有战斗力了,可惜就是没时间写文章了.在这段时间其实是遇到很多问题的,只是都是记录下来,并没有花时间去研究解决.但是这周遇到这个问题没办法让 ...
- winform 控件拖拽和缩放
核心类: using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using ...
- 【运维】使用Serv-U搭建FTP服务器
1.先安装好Serv-U,并作为系统服务安装 2.打开Serv-U,新建一个域 3.添加用户 4.解决阿里云专有网络的一个问题 遇到一个情景:需要使用Serv-U进行FTP更新软件,其中使用PASV的 ...
- String Algorithm Summary - 1
目录 Suffix Array Summay 单个字符串问题 两个字符串问题 多个字符串问题 AC-Automaton Summary 求长度为n(2e9)不包含给定字符串的合法串个数 包含至少一个词 ...
- flex 布局 实现三点筛子
实现麻将中三点筛子:效果如下图 具体实现代码: html代码: <div class="box"> <div class="item"> ...
- 【git】如何ignore一个文件的更改又保留其初始版本
参考: https://compiledsuccessfully.dev/git-skip-worktree/ https://stackoverflow.com/questions/9794931/ ...
- PHP 代码编写注意事项总结归纳
1- 编写模块化代码 良好的PHP代码应该是模块化代码.PHP的面向对象的编程功能是一些特别强大的工 具,可以把你的应用程序分解成函数或方法.你应该尽可能多的从你的应用程序的服务器端分开前端的HTML ...
- 解决(Oracle)ORA-12528: TNS: 监听程序: 所有适用例程都无法建立新连接 问题
解决(Oracle)ORA-12528: TNS: 监听程序: 所有适用例程都无法建立新连接 问题通过在CMD下用lsnrctl status 查看出的问题:发现BLOCKEDORACLE启动步骤:s ...
- git和svn的比较
当前的市场上主流的两种项目开发版本控制软件就是Git和SVN,那么这二者到底有什么区别呢? 在我们公司,其实两个都用,跟对个人体验,我觉得两者差不多,都是进行代码的版本管理. 我觉得1.由于我是实习生 ...