C_and_CPP 板


LINE

嗨 最近再看union的功能,有看过一些介绍与使用范例, 不过看了一些范例的使用方式,都没有目前想要实现的功能。 以下面例子为例 volatile union{ unsigned char Table_ALL[2]; union { unsigned char Byte ; struct { unsigned char C1_1:1; unsigned char C1_2:1; unsigned char C1_3:1; unsigned char C1_4:1; unsigned char C1_5:1; unsigned char C1_6:1; unsigned char C1_7:1; unsigned char C1_8:1; }BIT; } S1_C1; union { unsigned char Byte ; struct { unsigned char C2_1:1; unsigned char C2_2:1; unsigned char C2_3:1; unsigned char C2_4:1; unsigned char C2_5:1; unsigned char C2_6:1; unsigned char C2_7:1; unsigned char C2_8:1; }BIT; } S1_C2; }Table; Table.S1_C1.Byte看到的资料等同Table.Table_ALL[0] Table.S1_C2.Byte看到的资料等同Table.Table_ALL[1] 後来我想说是不是可以在里面宣告一个二维阵列 Table_ALL[2][2] 来使用呢? 因此我在里面把 Table_ALL[2] 改为 Table_ALL[2][2] 经过Debug模式看到原本的 S1_C1 是放在 Table_ALL[0][0], S1_C2 是放在 Table_ALL[0][1] 里面了没错 但是我该如何把在里面增加一个S2_C1放在Table_ALL[1][0]里面呢? 目前好像都没有找到这样的使用方式,不晓得有没有人用过。 最後感谢各位花时间看这个问题 :)) --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.216.1.168 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1577344159.A.770.html
1F:→ Lipraxde: 我觉得你的 union 跟我认识的不太一样... 12/26 19:03
2F:→ Lipraxde: S1_C1、S1_C2 应该在同个位置吧? 12/26 19:04
你指的同个位置是说,修改S1_C1会把S1_C2和Table_ALL[0]一起改掉吗? 我实际用Debug模式去看他是我上面说的那样,如果我再加一组S1_C3下去的话 他是跟S1_C1跟Table_ALL[0]共用同个记忆体的 不晓得关键字要下甚麽会比较多我要的例子,目前找很久都只有一般union的范例 :(( ※ 编辑: news9801 (115.43.145.7 台湾), 12/26/2019 21:13:34
3F:→ Lipraxde: 你的 debugger 真的这样显示?还是贴上来的 code 不一 12/26 22:14
4F:→ Lipraxde: 样? 12/26 22:14
5F:→ Lipraxde: https://ideone.com/5mUcus 12/26 22:23
Sorry 我刚刚在仔细看一次发现我少打了 这边补上连结 https://ideone.com/ZH9iLg
6F:推 johnpage: 宣告union结构,再用它当资料类型宣告阵列 12/27 02:38
※ 编辑: news9801 (61.216.1.168 台湾), 12/27/2019 08:35:09 刚刚我试了一下发现可以,我把程式的连结补在这边 感谢各位,用了一下ipraxde使用的ideone觉得满好用的XD https://ideone.com/Aye6ET ※ 编辑: news9801 (61.216.1.168 台湾), 12/27/2019 09:08:01
7F:→ Lipraxde: 这样跟宣告 union Sx_Cx var[a][b]; 好像没多少区别啊. 12/27 12:28
8F:→ Lipraxde: .. 12/27 12:28
Lipraxde说的这个部分有范例让我参考吗:D ※ 编辑: news9801 (61.216.1.168 台湾), 12/27/2019 14:50:29
9F:→ Lipraxde: 跟 johnpage 说的是同个意思: 12/27 16:26
10F:→ Lipraxde: https://ideone.com/fXtABq 12/27 16:26
11F:→ Lipraxde: 置底公告有推荐贴程式码的方法,有很多种 12/27 16:26
感谢两位的意见与Lipraxde的范例, 这边的话我可能没有说清楚,因为我需要每个Bit都是独立的, 得用不同名称做辨认,因此得一个一个建起来。 感谢两位的回答 :)) ※ 编辑: news9801 (61.216.1.168 台湾), 12/30/2019 09:02:21
12F:→ Lipraxde: 没看仔细XD,用 union 跟 table 放一起让我以为都是塞 12/30 18:31
13F:→ Lipraxde: 差不多的东西 12/30 18:31







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP