简体   繁体   English

指向C中的int指针数组的指针

[英]Pointer to an array of int pointers in C

I am a bit confused in accessing elements in an array of pointers. 我在访问指针数组中的元素时有点困惑。 Say I have an pointer int **score_cards and it points to an array of int pointers of size n. 假设我有一个指针int ** score_cards,它指向一个大小为n的int指针数组。 I want to find the sum of the integers pointed to by the elements of the array. 我想找到数组元素指向的整数之和。

I was thinking of doing: 我在考虑做:

int sum = 0;
int i;
for(i = 0;i<n;i++){
    sum = sum + *(*score_card+ i);
}

But this is wrong, while, the following is right: 但这是错误的,而以下是正确的:

int sum = 0;
int i;
for(i = 0;i<n;i++){
    sum = sum + *(*(score_card + i));
}

Now, I have two questions, since *score_cards points to the first element of the array, isn't the ith element *score_cards + i? 现在,我有两个问题,因为* score_cards指向数组的第一个元素,不是第i个元素* score_cards + i? ie the address of the first element + i? 即第一个元素+ i的地址? Also, why do we increment i by 1, and not sizeof(*int) ? 另外,为什么我们将i递增1而不是sizeof(* int)? Thanks in advance! 提前致谢!

Please remember that the shorthand syntax a[b] exists for *(a + b) and is exactly equal. 请记住, *(a + b)的缩写语法a[b]完全相同。 You shouldn't use the latter as it's somewhat illegible. 你不应该使用后者,因为它有点难以辨认。

since *score_cards points to the first element of the array 因为* score_cards指向数组的第一个元素

That's incorrect. 那是不对的。 score_cards points to the first element of the array, *score_cards is the first element of the array. score_cards指向数组的第一个元素, *score_cards是数组的第一个元素。 Thus the i -th element of the array is *(score_cards + i) or equally score_cards[i] . 因此,数组的第i个元素是*(score_cards + i)或者相同的score_cards[i]

Also, why do we increment i by 1, and not sizeof(*int)? 另外,为什么我们将i递增1而不是sizeof(* int)?

In C, when adding an integer to a pointer, the integer is implicitly multiplied by the size of the type pointed to. 在C中,当向指针添加整数时,整数隐式地乘以指向的类型的大小。 This is so that when a is an array of objects of some type, a[i] is the i -th element of that array. 这是因为当a是某种类型的对象数组时, a[i]是该数组的第i个元素。

score_cards points to the first element of the array, *score_cards is the first element of the array. score_cards指向数组的第一个元素,* score_cards是数组的第一个元素。

Pointer arithmetic is aware of sizes and therefore is not required to be scaled to the size of the type. 指针算术知道大小,因此不需要缩放到类型的大小。

*score_card doesn't point to the the first element of the array - it is the first element of the array. * score_card不指向数组的第一个元素 - 它数组的第一个元素。 So *score_card + 5 will be the first element of the array plus five - not the 6th element of the array. 所以*score_card + 5将是数组的第一个元素加上五个 - 而不是数组的第6个元素。

When you add a number to a pointer, the compiler automatically multiplies the number by the size of the thing being pointed to. 向指针添加数字时,编译器会自动将数字乘以指向的事物的大小。 So score_card + i "actually" means score_card + i*sizeof(*score_card) . 所以score_card + i “实际上”意味着score_card + i*sizeof(*score_card) (Of course, if you wrote that in the program, it would end up acting like score_card + i*sizeof(*score_card)*sizeof(*score_card) which isn't what you want) (当然,如果你在程序中写了这个,那最终会表现得像score_card + i*sizeof(*score_card)*sizeof(*score_card) ,这不是你想要的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM