左值所需错误 [英] Lvalue required error

查看:60
本文介绍了左值所需错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有指针时我写了下面code,

While working with pointers i wrote the following code,

int main()
{
    int a[]={10,20,30,40,50};
    int i;
    for(i=0;i<5;i++)
    {
        printf("\n%d",*a);
        a++;
    }
    return 0;
}

现在按我的理解,数组名本身就是C中的地址和完成的指针运算都是在这里是按我的知识是正确的。但是,当我尝试了c那么它是给我左值要求的错误运行$ C $。

Now as per my understanding array name itself is an address in c and the pointer arithmetic done is here is correct as per my knowledge. But when i try to run the code it is giving me "Lvalue Required" error.

那么,什么是因为之前这也是我所遇到的情况下这个错误是有存在的需要左值错误的确切原因。其次,为什么上的指针算术是不合法的在这里在这种情况下?

So what is the exact reason for occuring Lvalue required error because before this also i have come across situations where this error is there. Secondly why the arithmetic on the pointer is not legal here in this case?

推荐答案

您不能做 A ++ 的静态阵列上 - 这是不是一个左值。你需要在一个指针,而不是做。试试这个:

You can't do a++ on a static array - which is not an Lvalue. You need to do on a pointer instead. Try this:

int *ptr = a;
int i;
for(i=0;i<5;i++)
{
    printf("\n%d",*ptr);
    ptr++;
}

虽然在这种情况下,它可能会更好只使用索引:

Although in this case, it's probably better to just use the index:

int i;
for(i=0;i<5;i++)
{
    printf("\n%d",a[i]);
}

这篇关于左值所需错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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