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) 结构体每个成员相对于结构体首地址 ...
随机推荐
- SpringMVC架构实现原理
SpringMVC架构实现原理 一.SpringMVC介绍 Spring mvc是一个基于mvc的web框架.其中核心类是DispatcherServlet,它是一个Servlet,顶层是实现的Ser ...
- Django中object.all是什么东西
前言 先抛出一个问题.银行就相当于一个数据库,你去银行取钱存钱办卡销卡,是你告诉银行柜员方便还是你自己去操作电脑办卡取卡方便?(你还不一定会,假设自动存款机还没发明),object在数据库中就相当于银 ...
- HP Server BIOS实验报告
原文链接http://www.hpiss.com/3924.html 注意:红色字体为HP手册中查找到的资源,及个人感觉应该注意的一些信息,个人翻译的也为红字体,网络中自行查找到的资源,以及询问各位师 ...
- 网神SecVSS 3600漏洞扫描系统
网神SecVSS 3600漏洞扫描系统严格按照计算机信息系统安全的国家标准.相关行业标准设计.编写.制造.网神SecVSS 3600漏洞扫描系统可以对不同操作系统下的计算机(在可扫描IP范围内)进行漏 ...
- 接口调用post请求参数在body中
package com.ynhrm.common.utils; import com.alibaba.fastjson.JSONObject; import lombok.Data; import o ...
- vue.js实现点击后动态添加class及删除同级class
最近使用vue需要实现一个点餐选择商品规格的页面,需要通过vue动态的给被点击的元素添加class名字,使其变色,其他的删除class.如图: 开始在网上找了许多办法发现不是太好用,最后找到一个发现还 ...
- Codeforces #250 (Div. 2) B. The Child and Set
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011639256/article/details/28100041 题读错了啊... 一直跪,但刚 ...
- Raspberry Pi 开机启动QT程序
https://blog.csdn.net/coekjin/article/details/52498212 https://blog.csdn.net/dubuzherui/article/deta ...
- 安装FTP
yum install vsftpd -y cd /etc/vsftpd/ touch login.txt vim login.txt db_load -T -t hash -f /etc/vsftp ...
- 【牛客网-剑指offer】变态跳台阶
题目: 一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 考点: 递归和循环 分析: 台阶数 跳法 1 1 2 2 3 4 4 8 5 1 ...