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

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

问题描述

我学习指针运算,并有一段代码给我错误相当一段时间。任何帮助将不胜感激(我不能找到它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 ;

我无法理解我在代码块中遇到的错误。我得到这个语句。

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 ===|

在这段代码中,我必须迭代数组而不取消引用或使用 []

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.指向一个常量内存位置。
这是一个正常的指针,但它指向一个本质上是常量的变量(只读变量infact)。这意味着它指向的变量的值不能通过它改变。通常使用这样的常量变量。

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.指向内存位置的指针。
这是一个在整个程序中只能指向一个内存位置的指针。它指向的变量的值可以改变,但指针本身是常量。

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.常量指向常量内存位置的指针。
这是一个指针,它本身是常数,并且指向一个常量内存位置。它被声明为这样。

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天全站免登陆