函数作为参数传入,以此打印每个数组的值
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int a[5] = { 1,2,3,4,5 };
int b[5] = { 2,3,4,5,6 };
int c[] = { 3,4,5,6,7 };
int* arr[3] = { a,b,c };
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 5; j++)
{
printf("%d", *(arr[i] + j));
//printf("%d", arr[i][j]);同样可以实现操作
}
printf("\n");
}
return 0;
}
使用解*,同一片区域操作
char buffer[6];
memset(buffer, 0, 6);
buffer[0] = 2;
int32_t *power = (int32_t *)(buffer + 2);
*power = 4118;
printf("%d %d %d %d %d %d\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
使用解*,同一片区域操作
int data[50] = {0};
val *newval = (val *)data;
data[0] =30;
data[1] =40;
newval->a =10;
newval->b =20;
printf("data %d %d \n", data[0], data[1]);
printf("newval %d %d \n", newval->a, newval->b);
声明了一个指向 val
类型的指针 newval
,并将 data
数组的地址强制转换为指向 val
的指针。 newval
现在指向 data
数组的第一个元素,并假设每个 val
结构体占用与两个连续整数相同的内存空间(即8字节,如果每个整数占4字节)
使用&,操作同一片区域
int data[50] = {0};
val &newval = *(val *)data;
data[0] =30;
data[1] =40;
newval.a =10;
newval.b =20;
printf("data %d %d \n", data[0], data[1]);
printf("newval %d %d \n", newval.a, newval.b);
通常下面这么用
int data[50] = {0};
val *ptr = (val *)data;
val &newval = *ptr;
data[0] =30;
data[1] =40;
newval.a =10;
newval.b =20;
printf("data %d %d \n", data[0], data[1]);
printf("newval %d %d \n", newval.a, newval.b);
不使用引用和解*,不共用区域
val newval = *(val *)data;
data[0] =30;
data[1] =40;
newval.a =10;
newval.b =20;
printf("data %d %d \n", data[0], data[1]);
printf("newval %d %d \n", newval.a, newval.b);