二维数组、指针数组、数组指针、结构体位操作联合等,大家有兴趣的可以共同进步
/*二维数组在概念上是二维的,有行和列,但在内存中所有的数组元素都是连续排列的,它们之间没有“缝隙”。以下面的二维数组 a 为例:
int a[3][4] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} };
但在内存中,a 的分布是一维线性的,整个数组占用一块连续的内存:
int (*p)[4] = a;
a+i == p+i a = &a[0] a[0] = &a[0][0]
a[i] == p[i] == *(a+i) == *(p+i)
a[i][j] == p[i][j] == *(a[i]+j) == *(p[i]+j) == *(*(a+i)+j) == *(*(p+i)+j)
什么类型付相关类型的指针
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
unsigned int a : 4;
unsigned int b : 8;
unsigned int c : 16;
unsigned int d : 14; // 这里14bit超出u32,放在下个结构体内,实际结构体内只有28bit内存
} STRUCT0;
typedef struct a {
int a;
char b;
long c;
short d;
}STRUCT1;
typedef union b {
STRUCT0 h;
int hh;
} UNION;
int main()
{
int intbuf[2][3] = {0, 1, 2, 3, 4, 5};
char charbuf[2][3] = {'a', 'b', 'c', 'd', 'e', 0};
int *inbuf[2];
inbuf[0] = intbuf[0];
inbuf[1] = intbuf[1];
printf("%p,%p,%p,%d,%d,%d\n", \
inbuf[0], inbuf[0] + 1, inbuf[1], *(inbuf[0] + 1), *inbuf[0], *inbuf[1]);
printf("%d\n", sizeof(inbuf[0]));
char *chbuf[2];
chbuf[0] = charbuf[0];
chbuf[1] = charbuf[1];
printf("%p,%p,%p,%d,%c,%d\n", \
chbuf[0], chbuf[0] + 1, chbuf[1], *(chbuf[0] + 1), *chbuf[0], *chbuf[1]);
printf("%d\n", sizeof(chbuf[0]));
// int *ibuf[] ibuf[][]
int (*ibuf)[3];
char (*cbuf)[3];
ibuf = intbuf;
cbuf = charbuf;
printf("%p,%p,%p,%d,%d,%d\n", \
ibuf[0], ibuf[0] + 1, ibuf[1], *(ibuf[0] + 1), *ibuf[0], *ibuf[1]);
printf("%p,%p,%p,%s,%d,%d\n", \
cbuf[0], cbuf[0] + 1, cbuf[1], cbuf[0], *cbuf[0], *cbuf[1]);
printf("%d,%d\n", sizeof(cbuf), sizeof(cbuf[0]));
// int (*iibuf)[2];err
int **iibuf;
// char (*ccbuf)[2];err
char **ccbuf;
iibuf = inbuf;
ccbuf = chbuf;
printf("%p,%p,%p,%d,%d,%d\n", \
iibuf[0], iibuf[0] + 1, iibuf[1], *(iibuf[0] + 1), *iibuf[0], *iibuf[1]);
STRUCT0 t0 = {
.a = 0xf,
.d = 0xf,
.c = 0xfeef,
.b = 0xff
};
// 寄存器修改相应字段
UNION t1 = {
.h = t0
};
printf("%d,%d,%d,%d,0x%x\n", t1.h.a, t1.h.b, t1.h.c, t1.h.d, t1.hh);
t1.hh = 0xffffffff;
printf("%d,%d,%d,%d,0x%x\n", t1.h.a, t1.h.b, t1.h.c, t1.h.d, t1.hh);
return 0;
}