小端环境输出结果为 value = 0x00004043
typedef struct TestData_ {
unsigned int data1 : 2;
unsigned int data2 : 12;
unsigned int data3 : 2
} TestData;
int main(void)
{
TestData data;
memset(&data, 0, sizeof(data));
data.data1 = 0x7;
data.data2 = 0x10;
data.data3 = 0x1;
unsigned int result = *(unsigned int *)&data;
printf("value = 0x%.8x", result);
//printf("%p\n", &data.data2); // compile error
// Address of bit-field requested
return 0;
}
.
计算如下,按照位域分配为:
11 0000 1000 0000 10
整理为:
1100 0010 0000 0010
即为:
4043
C语言结构体中的冒号表示位域
https://blog.csdn.net/yihongxiaoxiang/article/details/50327587
一个char*类型的指针,恰好指向了一个int,想让这个指针跳过int指向下一个char,下面的代码可以达到这个目的的是(B)
A: (int *)p)++;
B: p += sizeof(int);
A: Assignmemt to cast is illegal, lvalue casts are not supported
char* p0 = "hello";
char* p1 = p0;
char* p2 = p1;
((int *)p1)++;
p2 += sizeof(int);
以下程序的输出为:
void func1(int a[5]) // int* const a
{
printf("a size is %d\n", sizeof(a));
a[4] = 1;
}
typedef int A[5]; // A is int[5]
typedef A* PA; // int (*)[5]
int main(void)
{
int a[5] = {0};
func1(a);
A a1;
func1(a1);
PA b = &a1;
func1(*b);
printf("size %d\n", sizeof(A));
return 0;
}
a size is 4
a size is 4
a size is 4
size 20
下面代码输出为
// a.h
typedef int DevId;
// a.c
#include "a.h"
typedef int DevId;
// b.c
#include "a.h"
typedef short DevId; // compile error
// c.c
#include "a.h"
void Func(void) {
typedef char DevId;
DevId x;
printf("%d", sizeof(x)); // 1
}
a.c和c.c会起作用
int (*p[10])(int*)声明了p是一个(C)
A. 指向函数的指针
B. 返回指针的函数
C. 元素为函数指针的数组
D. 入参为数组指针的函数
在X64环境下,调用如下函数(假设xs的地址是0x10000),输出是 3 5 0x10018
int main(void)
{
// sizeof(int) = 4
int xs[][2] = {0, 1, 2, 3, 4, 5,};
int *p1 = &xs[1][0];
printf("%p\n", xs);
printf("%p\n", &xs); // 和xs相同
printf("%p\n", xs + 1); // 移动一个列长 xs+0x08
printf("%p\n", &xs + 1); // 移动整个数组的大小 xs+0x18
printf("%p\n", *(xs + 1)); // 移动一个列长 xs+0x08
printf("%d\n", **(xs + 1)); // 2
printf("%u %u %p\n", p1[1], *(*(xs + 2) + 1), &xs+1); // 3 5 xs+0x18
return 0;
}
执行下列语句后的结果为(x=3,y=3)
int x = 3, y;
int *px = &x;
y = *px++;
printf("%d %d", x, y); // 3 3
以下代码的输出为:
typedef struct haha
{
int haha1;
double haha2;
};
int main()
{
haha *h1 = NULL;
// sizeof是在编译期计算的,所以引用空指针并非问题。
cout << sizeof(*h1) << endl; // 16
}
END