使用指针跨越整数数组的所有元素 [英] Using pointer for crossing over all elements in INTEGER array

查看:122
本文介绍了使用指针跨越整数数组的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用指针越过整数数组的所有元素(类同用指针字符串元素越过)。我知道整数数组不为null终止的,所以当我尝试使用指针溢出跨越阵列我。所以加NULL作为数组的最后一个元素和它的工作就好了。

Is there a way to cross over all elements in integer array using pointer ( similiar to using pointer to cross over string elements).I know that integer array is not NULL terminated so when I try to cross over array using pointer it overflows.So I added NULL as a last element of an array and it worked just fine.

int array[7]={1,12,41,45,58,68,NULL};
int *i;
for(i=array;*i;i++)
    printf("%d ",*i);

但是,如果在数组中的元素之一是0,这将只是表现为NULL.Is那里,将实现道口指针放在整数数组的所有元素任何其他方式?

But what if one of the elements in array is 0 ,that will behave just as NULL.Is there any other way that will implement pointer in crossing over all elements in integer array?

推荐答案

这是你可以在使用指针C风格的字符串迭代的原因是,在256个不同的字符值,一种是专门预留的跨preTED为这是字符串的结束。正因为如此,C风格的字符串不能空字符存储在其中的任何地方。

The reason that you can iterate across a C-style string using pointers is that of the 256 different character values, one has been specifically reserved to be interpreted as "this is the end of the string." Because of this, C-style strings can't store null characters anywhere in them.

当你想使用整型数组类似的把戏,你发现了同样的问题。如果您希望能够停止在某些时候,你必须挑选一些整数,并保留它的意思是这不是一个整数,它真的整数序列的结束。所以,不,还有就是采取一个整数数组,除非你愿意选择那些不能正常显示字符串中的某个值由一个特殊值标定到底有没有通用的方法。

When you're trying to use a similar trick for integer arrays, you're noticing the same problem. If you want to be able to stop at some point, you'll have to pick some integer and reserve it to mean "this is not an integer; it's really the end of the sequence of integers." So no, there is no general way to take an array of integers and demarcate the end by a special value unless you're willing to pick some value that can't normally appear in the string.

C ++选择了一种不同的方法比C划定序列。相反,存储单元用某种空终止,C ++的 - 风格的范围(比如你会在矢量找到字符串列表)存储两个迭代器,开始()端() ,指示过去末端的第一元件和第一元件。您可以通过编写在这些范围内迭代

C++ opted for a different approach than C to delineate sequences. Instead of storing the elements with some sort of null terminator, C++-style ranges (like you'd find in a vector, string, or list) store two iterators, begin() and end(), that indicate the first element and first element past the end. You can iterate over these ranges by writing

for (iterator itr = begin; itr != end; ++itr)
    /* ... visit *itr here ... */

此方法比C字符串的方法来确定,因为它不依赖于在范围内的任何值的特定属性范围灵活得多。我建议选择加入,如果你想迭代一个整数范围内的使用这样的事情。它是更明确的了解范围的边界,并且不碰上,其中一定值不能被存储在范围怪异的问题。

This approach is much more flexible than the C-string approach to defining ranges as it doesn't rely on specific properties of any values in the range. I would suggest opting to use something like this if you want to iterate over a range of integer values. It's more explicit about the bounds of the range and doesn't run into weird issues where certain values can't be stored in the range.

这篇关于使用指针跨越整数数组的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆