求职_第1篇 程序设计基础及数据机构基础_第3章 结构体、共用体与枚举

897

3.1结构体struct

 

例1:关于struct和class,下列说法正确的是。

 

struct的成员默认是public,class的成员默认是private

 

3.1.1结构体的定义

 

例1:已知:

 

struct st
{
	int n;
	struct st*next;
};

static struct st a[3] = { 1, &a[1], 2, &a[2], 3, &a[0] }, *p;

 

如果下列语句的显示是2,则对p的赋值是。

 

	p = &a[2];
	printf("%d\n", ++(p->next->n));

 

3.1.2结构体中的位字段

 

例1:写出下列代码的输出结果?

 

1,7,15
0,3,15
请按任意键继续. . .

 

#include <stdio.h>
#include <windows.h>

struct bs
{
	unsigned a : 1;
	unsigned b : 3;
	unsigned c : 4;
}bit, *pbit;

int main()
{
	bit.a = 1;
	bit.b = 7;
	bit.c = 15;

	printf("%d,%d,%d\n", bit.a, bit.b, bit.c);

	pbit = &bit;

	pbit->a = 0;
	pbit->b &= 3;
	pbit->c |= 1;

	printf("%d,%d,%d\n", pbit->a, pbit->b, pbit->c);

	system("pause");

	return 0;
}

 

3.2共用体union

 

例1:对结构体和共用体占用内存描述正确的是。

 

结构体占用内存,可能超过各成员所需要的内存量总和

共用体占用内存为各成员中占用最大者内存

 

例2:在小端系统中,以下代码输出什么?

 

输出结构为32 20。

 

#include <stdio.h>
#include <windows.h>

union Student
{
	int i;
	unsigned char ch[2];
};

int main()
{
	union Student student;
	student.i = 0x1420;

	printf("%d %d\n", student.ch[0], student.ch[1]);

	system("pause");

	return 0;
}

 

还可以以如下方式判断大小端:

 

#define BIG_ENDIAN 0
#define LITTLE_ENDIAN 1

int TestByteOrder()
{
	short int word = 0x0001;
	char *byte = (char *)&word;
	return (byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
}

 

例3:假设在一个32位环境,CPU位Little Endian模式,所有参数用栈传递,则执行以下程序,其结果是?

 

1 0 2

 

int main()
{
	long long a = 1, b = 2, c = 3;

	printf("%d %d %d\n", a, b, c);

	system("pause");

	return 0;
}

 

例4:32位小端字节序的机器上,如下代码:

 

0x201,0x605,0x4030201,0x8070605
请按任意键继续. . .

 

int main()
{
	char array[12] = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };
	short *pshort = (short *)array;
	int *pint = (int *)array;
	long long *pint64 = (long long *)array;

	printf("0x%x,0x%x,0x%x,0x%x\n", *pshort, *(pshort + 2), *pint64, *(pint + 1));

	system("pause");

	return 0;
}

 

例5:在32位小端机器上,下面程序的输出结果为。

 

struct Test
{
	unsigned short int a : 5;
	unsigned short int b : 5;
	unsigned short int c : 6;
};

int main()
{
	struct Test test;
	test.a = 16;
	test.b = 4;
	test.c = 0;
	int i = *(short *)&test;
	printf("%d\n", i);

	system("pause");

	return 0;
}

 

例6:Intel X86 PC上运行以下程序,其输出值是。

 

7。

 

struct Test
{
	unsigned short int a : 5;
	unsigned short int b : 5;
	unsigned short int c : 6;
};

int main()
{
	union
	{
		struct
		{
			unsigned short s1 : 3;
			unsigned short s2 : 3;
			unsigned short s3 : 2;
		}x;
		char c;
	}v;

	v.c = 103;

	std::cout << v.x.s1 << std::endl;

	system("pause");

	return 0;
}

 

3.3枚举

 

例1:下列代码的输出为多少?

 

a=0,b=5,c=6,d=4,e=5
h=0,x=1,y=2,z=3,v=120,w=121,r=99,s=100,t=101
请按任意键继续. . .

 

int main()
{
	enum { a, b = 5, c, d = 4, e };
	enum { h, x, y, z, v = 120, w, r = 99, s, t };

	printf("a=%d,b=%d,c=%d,d=%d,e=%d\n", a, b, c, d, e);
	printf("h=%d,x=%d,y=%d,z=%d,v=%d,w=%d,r=%d,s=%d,t=%d\n", h, x, y, z, v, w, r, s, t);

	system("pause");

	return 0;
}

 

例2:请写出enume的声明方式。

 

enum 枚举类型名 {枚举常量1[=整型常数], 枚举常量2[=整型常数], ...} [变量名列表]

 

3.4sizeof运算符

 

3.4.1sizeof的使用方法

 

例1:求sizeof(2), sizeof(2+3.14)的各自大小?

 

4,8

 

例2:我们来看一个完整的例子:

 

sizeof(foo())=1
请按任意键继续. . .

 

char foo()
{
	printf("foo() has been called.\n");
	return 'a';
}

int main()
{
	int sz = sizeof(foo());
	printf("sizeof(foo())=%d\n", sz);

	system("pause");

	return 0;
}

 

3.4.2sizeof的结果

3.4.3与strlen()比较

 

3.4.4指针、引用、汉字及数组的sizeof操作

 

例1:在32位系统下,int *p;

 

sizeof(p)=4;

 

例2:下列的代码的输出结果是什么?

 

24

 

int main()
{
	int a[6] = { 0,2,4,6,8,10 };
	int(&p)[6] = a;

	printf("%d\n", sizeof(p));

	system("pause");

	return 0;
}

 

例3:"Abc汉字"的所占内存长度为。

 

8

 

例4:

 

40 6 8
请按任意键继续. . .

 

int main()
{
	int a[10];
	char b[] = "hello";
	int *c = new int[50];

	printf("%d %d %d\n", sizeof(a), sizeof(b), sizeof(c));

	system("pause");

	return 0;
}

 

例5:下述代码中c1与c2的值分别是什么?

 

8
8
请按任意键继续. . .

 

void fun1(char a1[3])
{
	int c1 = sizeof(a1);
	printf("%d\n", c1);
}

void fun2(char a2[])
{
	int c2 = sizeof(a2);
	printf("%d\n", c2);
}

int main()
{
	char str[3] = "ab";

	fun1(str);
	fun2(str);

	system("pause");

	return 0;
}

 

例6:32位机器上,定义int **a[3][4],这个数组占多大的空间。

 

32位

3x4x4=48

 

例7:在32位操作系统中,我们定义如下变量:

 

8
请按任意键继续. . .

 

int main()
{
	int(*n)[10];

	printf("%d\n", sizeof(n));

	system("pause");

	return 0;
}

 

3.4.5struct的空间计算

 

例1:在Windows 32环境下,下述代码中语句1与语句2的输出是什么?

 

24

16

 

struct s1
{
	char a;
	double b;
	int c;
	char d;
};

struct s2
{
	char a;
	char b;
	int c;
	double d;
};

int main()
{
	std::cout << sizeof(s1) << std::endl;
	std::cout << sizeof(s2) << std::endl;

	system("pause");

	return 0;
}

 

例2:某计算机存储器按字节编制,采用小端方式存放数据。假定编译器规定int和short型长度分别为32位和16位,并且数据按边界对齐存储。某C语言程序段如下:

 

struct
{
	int a;
	char b;
	short c;
}record;

int main()
{
	record.a = 273;

	system("pause");

	return 0;
}

 

若record变量的首地址为0xC008,则地址0xC008中内容及record.c的地址为

0x11、0xC00E

 

1.含结构体的结构体的空间计算

 

例1:下述代码的输出结果是什么?

 

8
16
请按任意键继续. . .

 

struct S3
{
	char c;
	int i;
};

struct S4
{
	char c1;
	S3 s;
	char c2;
};

int main()
{
	std::cout << sizeof(S3) << std::endl;
	std::cout << sizeof(S4) << std::endl;

	system("pause");

	return 0;
}

 

2.含数组的结构体的空间计算

 

例1:sizeof(s1)=?

 

12

 

struct s1
{
	char a[8];
	int b;
};

 

例2:以下代码的输出结果是什么?

 

8
8
9
16
请按任意键继续. . .

 

struct s1
{
	char a[8];
};

struct s2
{
	double b;
};

struct s3
{
	char a;
	s1 s;
};

struct s4
{
	s2 s;
	char a;
};

int main()
{
	std::cout << sizeof(s1) << std::endl;
	std::cout << sizeof(s2) << std::endl;
	std::cout << sizeof(s3) << std::endl;
	std::cout << sizeof(s4) << std::endl;

	system("pause");

	return 0;
}

 

3.含位域结构体的空间计算

famfkma

转载于:https://www.cnblogs.com/denggelin/p/6847761.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值