* p = array [i]的效果?

时间:2015-09-12 19:32:17

标签: c arrays pointers variable-assignment

我见过类似于

的作业
*p = array[i];

在代码库中,我想知道这对指针有什么影响。如果以前曾经问过这个问题,我很抱歉,但搜索引擎一直在吃我的特殊字符,可能会妨碍我的搜索。

1 个答案:

答案 0 :(得分:1)

你的问题不清楚,因为没有给出足够的细节。 正如评论中*p = array[i]解除引用p所述,并将array[i]处的内容分配给任何p点。

示例1

int i = 0, j;
int *p = &j; // p is a pointer to integer and now it points to j
int array[2] = { 4, 2 };
*p = array[i]; // this means j = array[0] so j = 4 and j is 4 now

示例2

int i = 0, *j, k, l;
int **p = &j; // p is a pointer to pointer to integer and now it points to j
int* array[2] = { &k, &l };
*p = array[i]; // this means j = array[0] so j = &k and j points now to k