看着我的Previous question。我用这组代码获得了解决方案
const MEMBERS *some_members = (MEMBERS *) GET_MEM();
unsigned value1 = some_members->mem1;
unsigned value2 = *some_members->mem2;
不按预期工作。但是在样本是
的情况下unsigned sample [4] = {2,5,6,1};
如果需要从结构中对数组的第3个字符(即sample [3] = 1。)进行索引,如何在数组中进行索引。我也将value2作为指针吗?
谢谢大家..
答案 0 :(得分:1)
在这种情况下,是的,使value2
成为指针。即:
const MEMBERS *some_members = (MEMBERS *) GET_MEM();
unsigned value1 = some_members->mem1;
unsigned *value2 = some_members->mem2;
// These are true statements
value2[0] == 2;
value2[1] == 5;
value2[2] == 6;
value2[3] == 1;
答案 1 :(得分:1)
可以访问指针和数组。
例如:
int array[4] = { 1, 2, 3, 4 };
printf("Third entry in array = %d\n", array[2]);
int *pointer = array;
printf("Third entry in array using pointer = %d\n", pointer[2]);
如果指针位于某个结构中并不重要,只需使用普通字段访问权限(例如structure.pointer[2]
或structpointer->pointer[2]
)。