通过C数组迭代不使用指针 [英] Iterating through C-style array not using a pointer

查看:156
本文介绍了通过C数组迭代不使用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习指针运算,并有一张code的给我的错误很长一段时间。任何帮助将是AP preciated。(我想不出找到它的SO)

I am learning pointer arithmetic and have a piece of code giving me error for quite some time. any help would be appreciated.(I couldnt find it on SO)

int arr [] = {1, 2, 3, 4, 5} ;
for (int i = 0 ; i < 5 ; i++)
{
    cout << *arr ;
    arr++ ;
}
cout << *arr << endl ;

我无法理解我在$ C $的cblock得到错误。我收到此声明。

I am unable to understand the error i am getting in codeblocks. I am getting this statement.

    error: lvalue required as increment operand|
    ||=== Build finished: 1 errors, 0 warnings ===|

在此code,我必须遍历数组,而不提领或使用 [] 运营商。

In this code i have to iterate the array without dereferencing or using [] operator.

推荐答案

当你试图增加数组您收到此错误。它是无效的,因为在C ++中,数组名可以隐式转换为一个常量指针的第一指标。你不能增加一个数组,因为数组是一个容器,递增的容器是没有意义的。

You are getting this error as you trying to increment the array. It is invalid because in c++, the name of the array can be implicitly converted to a constant pointer to the very first index. You cannot increment an array, because an array is a container and incrementing a container makes no sense.

要完全回答你的问题,我需要解释一些东西。让我尝试和其他人可以补充。

To answer your question completely, I would need to explain some things. Let me try and others can add in.

记得有三种类型的常量指针。

Remember there are three types of constant pointers.

1.Pointer至恒定存储位置。
这是一个正常的指针,但它指向一个变量,它在本质上是恒定(只读变量逸岸)。这意味着变量它指向的值无法通过它进行更改。通常它是用来点的常数变量是这样的。

1.Pointer to a constant memory location. This is a normal pointer but it points to a variable which is constant by nature (read only variable infact). It means that the value of variable it is pointing to cannot be changed through it. Normally it is used point a constant variable like this.

const int x = 10 ;
const int *ptr = & x ; 

在这里,你不能做 * PTR = 5; ,因为指针指向一个常变量

here, you cannot do *ptr = 5 ; because the pointer is pointing to a constant variable.

2.Const指针的内存位置。
这是可以指向只有一个存储器位置在整个程序的指针。它指向该变量的值可以改变,但指针本身是恒定的。据声明如下。

2.Const pointer to a memory location. This is a pointer which can point to only one memory location throughout the program. The value of the variable it is pointing to can be changed but the pointer itself is constant. It is declared like this.

int x = 10, y = 5 ;
int * const ptr = &x ;

你不能做 PTR =安培; Y; ​​后面的程序。数组可以隐式转换为一个常量指针到内存位置为好。因此它不能被递增或以这种方式递减。
(你可以,如果你喜欢在这里读到它:
什么阵列腐烂?

you cannot do ptr = &y ; later in the program. Array can be implicitly converted to a constant pointer to a memory location as well. So it cannot be incremented or decremented in this way. (You can read about it here if you like: what is array decaying?)

3.Constant指针为恒定的内存位置。
这是一个指向它是恒定本身和指向常数存储位置为好。现声明如下。

3.Constant pointer to a constant memory location. This is a pointer which is constant itself and points to a constant memory location as well. it is declared like this.

const int x = 8 ;
const int * const ptr = &x ;

这意味着,无论是你可以将指针指向任何地方,除了初始化的位置,你甚至不能改变它指向的位置的值。

It means that neither you can point the pointer to anywhere except the initialized location, and you cannot even change the value of the location it is pointing to.

我希望这个概念现在已经非常清楚了。现在,你的问题:要打印,通过指针运算数组,但不能增加数组本身。所以,一个解决办法做到这一点是让另一个指针(正常的),并为其分配数组的第一个索引的地址。然后你就可以增加,减少或更改阵列的价值。事情是这样的。

I hope the concept is very much clear by now. Now to your question: you want to print the array through pointer arithmetic, but you cannot increment the array itself. So one solution to do this is to make another pointer (a normal one) and assign it the address of the array’s first index. Then you can increment, decrement or change the value of that array. Something like this.

int arr [] = {1, 2, 3, 4, 5} ;
int *ptr = arr ;
for (int i = 0 ; i < 5 ; i++)
{
    cout << *ptr << " " ;
    ptr++ ;
}

这篇关于通过C数组迭代不使用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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