您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

解释C语言中的联合指针

2025/5/5 6:11:07发布42次查看
联合是由不同数据类型的多个变量共享的内存位置。
语法c 编程中指向联合的指针的语法如下 -union uniontag{ datatype member 1; datatype member 2; ---- ---- datatype member n;};
示例下面的示例展示了结构体并集的用法。
union sample{ int a; float b; char c;};
联合变量的声明以下是联合变量的声明。它有三种类型,如下所示−
类型1union sample{ int a; float b; char c;}s;
type 2 的翻译为:type 2union{ int a; float b; char c;}s;
type 3 的翻译为:type 3union sample{ int a; float b; char c;};union sample s;
当声明联合时,编译器会自动创建最大尺寸的变量类型来容纳联合中的变量。
任何时候只能引用一个变量。
使用相同的结构语法来访问联合成员。
点操作符用于访问成员。
箭头操作符(->)用于使用指针访问成员。
我们可以使用指向联合的指针,并使用箭头操作符(->)来访问成员,就像结构体一样。
示例下面的程序展示了在c编程中使用指向联合的指针的用法 -
live demo
#include <stdio.h>union pointer { int num; char a;};int main(){ union pointer p1; p1.num = 75; // p2 is a pointer to union p1 union pointer* p2 = &p1; // accessing union members using pointer printf("%d %c", p2->num, p2->a); return 0;}
输出当上述程序被执行时,它产生以下结果 −
75 k
示例 2考虑具有不同输入的同一示例。
 实时演示
#include <stdio.h>union pointer { int num; char a;};int main(){ union pointer p1; p1.num = 90; // p2 is a pointer to union p1 union pointer* p2 = &p1; // accessing union members using pointer printf("%d %c", p2->num, p2->a); return 0;}
输出当上述程序被执行时,它产生以下结果 −
90 z
以上就是解释c语言中的联合指针的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product