通过与指针数组循环? [英] Loop through an array with pointers?
本文介绍了通过与指针数组循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想了解指针这是一个真正皮塔饼时,你不太完全了解它。
I am trying to learn pointers which is a real PITA when you don't quite fully understand it.
我要打印的阵列中的所有元素,但只有在打印的第一要素。
I am trying to print all the elements in the array but only the first element is printed.
#include <stdio.h>
int count(const int* numbers, int size)
{
for(; numbers < size; numbers++)
{
printf("%d", *numbers);
}
}
int main(void)
{
int numbers[] = {3, 4, 6, 3, 46};
int result = count(numbers, 5);
printf("%d\n", result);
return 0;
}
在循环中的计数的功能似乎并没有正常工作,因为它只能通过一次循环,但我不明白为什么。
The loop in the count function does not seem to work properly since it is only looping through one time but I can't understand why.
推荐答案
比较数字&LT;大小
是错误的。
您有3种选择:
-
有一个单独的索引变量:
Have a separate index variable:
int count;
for(count = 0; count < size; count++)
{
printf("%d", numbers[count]);
}
有一个单独的光标指针:
Have a separate "cursor" pointer:
const int * const end = numbers + size;
for(; numbers < end; numbers++)
{
printf("%d", *numbers);
}
递减尺寸
:
for(; size != 0; size--, numbers++)
{
printf("%d", *numbers);
}
这篇关于通过与指针数组循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文