叙述原因:

union data{

int a;
double b;
};

对于union,实际中用的并不多,之前也知道怎样计算union的单元(在字对齐的基础上取最大成员所占的内存大小),比如

union data{

int a;
char b[5];
};  最大为5个字节,但为了字对齐,需要是4的倍数,sizeof(data)大小就是8,其他复杂的结构大家可以网上搜一搜,一般考的可能就是这样。

问题1:

union各数据单元的内存地址是否相同?

本人设想拥有最大元素的内存单元,那么各元素的地址是相同的。结果也验证了正确性。

union data{

int a;
char b;
double c;
char ch[5];
};

union data d;
d.a = 10;
printf("%d %d %d %d\n",&d,&d.a,&d.b,&d.c,d.ch); 4个结果全部相同。

问题2:

union data{

int a;
double b;
}; 如果设置了d.a = 10,那么d.b = ???

是不是有点蒙?如果按照 内存地址相同,说明是共享了同一块数据单元, d.b的值应该也是 10

但是 d.b != 10 。

同理如果设置了d.b = 10,那么d.a =?? 结果是d.a = 0

但是对于特殊情况,如果data的结构是

union data{

int a;
char b;
};

如果设置了d.a = 10,那么d.b =??? 其实这个时候 a.b = 10 原因是 这两个类型在C语言中的内存结构是一样的,但是 int float double却不相同。

这也就是要强调 对于结构体你用的什么赋值,就采用哪一个变量,否则值可能得不到。。

我的想法是: 他们内存的数据是一样,但是不同类型读取数据的方式不同,导致了数据与赋值数据不同。主要是数据的读取方式和对内存数据的组织方式

C/C++ union的更多相关文章

  1. SQL Server-聚焦UNIOL ALL/UNION查询(二十三)

    前言 本节我们来看看有关查询中UNION和UNION ALL的问题,简短的内容,深入的理解,Always to review the basics. 初探UNION和UNION ALL 首先我们过一遍 ...

  2. SQL 提示介绍 hash/merge/concat union

    查询提示一直是个很有争议的东西,因为他影响了sql server 自己选择执行计划.很多人在问是否应该使用查询提示的时候一般会被告知慎用或不要使用...但是个人认为善用提示在不修改语句的条件下,是常用 ...

  3. LINQ to SQL语句(8)之Concat/Union/Intersect/Except

    适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1.简单形式: var q = ( from c in db ...

  4. SQLServer-----Union,Union All的使用方法

    转载: http://blog.csdn.net/kiqinie/article/details/8132485 select a.Name from Material as a union sele ...

  5. 假如 UNION ALL 里面的子句 有 JOIN ,那个执行更快呢

    比如: select id, name from table1 where name = 'x' union all select id, name from table2 where name =  ...

  6. sql union和union all的用法及效率

    UNION指令的目的是将两个SQL语句的结果合并起来.从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION的一个限制是两个 ...

  7. 【oracle】union、union all、intersect、minus 的用法及区别

    一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...

  8. sql with as union all

    WITH RPL (FId,Fname,Forder) AS ( SELECT ment.deptno,ment.deptname,ment.orderno FROM JTERP..fg_depart ...

  9. Oracle 中 union 和union all 的简单使用说明

    1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...

  10. LINQ系列:LINQ to SQL Concat/Union

    1. Concat 单列Concat var expr = (from p in context.Products select p.ProductName) .Concat( from c in c ...

随机推荐

  1. [Angular 2] Understanding Pure & Impure pipe

    First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...

  2. python selenium自动化(二)自动化注册流程

    需求:使用python selenium来自动测试一个网站注册的流程. 假设这个网站的注册流程分为三步,需要提供比较多的信息: 在这个流程里面,需要用户填入信息.在下拉菜单中选择.选择单选的radio ...

  3. _vsnprintf 用法

    _vsnprintf,C语言库函数之一,属于可变参数.用于向字符串中打印数据.数据格式用户自定义. 头文件: #include <stdarg.h> 函数声明: int _vsnprint ...

  4. Logistic回归总结

    原文:http://blog.csdn.net/dongtingzhizi/article/details/15962797  Logistic回归总结 作者:洞庭之子 微博:洞庭之子-Bing (2 ...

  5. c实例_挑战程序竞赛,蚂蚁

    #include <stdio.h> //蚂蚁的题目 int max(int a,int b) { int count; count=a>b?a:b; return count; } ...

  6. UITableViewCell高度自适应探索--AutoLayout结合Frame

    UITableViewCell高度自适应探索--UITableView+FDTemplateLayoutCell地址: http://www.jianshu.com/p/7839e3a273a6UIT ...

  7. Jessica's Reading Problem

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  8. js函数中的几个特点

    定义函数有两种方式:函数声明 函数表达式 1.函数声明是这样的: function box(arg0,arg1,arg2){ //函数体} 关于函数声明有一个重要的特征:函数声明提升,也就是说执行代码 ...

  9. C中的回调函数

    C语言中应用回调函数的地方非常多,如Nginx中: struct ngx_command_s { ngx_str_t name; ngx_uint_t type; char *(*set)(ngx_c ...

  10. The Story of self Parameter in Python, Demystified

      转自:http://www.programiz.com/article/python-self-why If you have been programming in Python (in obj ...