0

看着我的上一个问题。我用这组代码得到了解决方案

const MEMBERS *some_members = (MEMBERS *) GET_MEM();
unsigned value1 = some_members->mem1;
unsigned value2 = *some_members->mem2;

没有像我预期的那样工作。但是在样本是

unsigned sample [4] = {2,5,6,1};

如果我需要从结构中对数组的第三个字符(即 sample [3] =1. )进行索引,我该如何在数组中进行索引。我是否也将 value2 设为指针?

谢谢大家。。

4

2 回答 2

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;
于 2013-02-01T12:22:47.520 回答
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]or structpointer->pointer[2])。

于 2013-02-01T12:23:48.417 回答