buf [n] ++是左值吗? [英] Is buf[n]++ a lvalue?

查看:138
本文介绍了buf [n] ++是左值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码给出了错误C2105:'''''''需要l值。


char buf [20];

buf [n ] ++ =''A''; // buf [n] ++ isn''ta lvalue


为什么buf [n] ++不是左右?这是根据C标准还是

这样的原因?


如果buf [n] ++不是左值,那么为什么以下代码有效吗?


void func(char * a,char * b)

{

while(* a ++ = * b ++){}

}

解决方案

Stub< st ** @ asof.com>这样说:

char buf [20];
buf [n] ++ =''A''; // buf [n] ++ isn''ta左值
为什么buf [n] ++不是左值?这是根据C标准还是存在某种原因?
如果buf [n] ++不是左值,那么为什么以下代码有效?
while(* a ++ = * b ++){}




因为* a ++与*(a ++)相同。 a是一个指针,所以

增加它是好的。然而,buf [n]是一个你无法增加的角色。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


Stub写道:

以下代码给出了错误C2105:''''''''需要l值。

char buf [20];
buf [n] ++ =''A''; // buf [n] ++ isn''ta lvalue

为什么buf [n] ++不是左右吗?这是根据C标准还是存在某种原因?


为什么你认为这两个选项是相互排斥的?

;-)。 C标准*中的事情是有原因的!


然而,最有可能的表达方式是:


buf [ n ++] =''A'';

如果buf [n] ++不是左值,那么为什么以下代码有效?

void func(char * a,char * b)
{while /(* a ++ = * b ++){}
}




因为这里*指针*被解除引用(* a是左值,

是吗?),执行赋值(产生表达式的值)

最后每个指针都递增。


HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀

哦,对于常规老垃圾邮件的美好时光。


Christopher Benson-Manica写道:

但是,buf [n]是一个你无法增量的角色。




是的,你可以。

#include< stdio.h>


int main(无效)

{

char buf [] =" 3.1415926" ;;

int n = 0;

buf [n] ++;

printf("%s \ nn",buf);

返回0;

}


-

Richard Heathfield: bi ****@eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


The following code gives error C2105: ''++'' needs l-value.

char buf[20];
buf[n]++ = ''A'';//buf[n]++ isn''t a lvalue

Why buf[n]++ isn''t a lvalue? Is this according to C Standard or is there
some reason for this?

If buf[n]++ isn''t a lvalue, then why the following code works?

void func(char *a, char *b)
{
while(*a++=*b++){}
}

解决方案

Stub <st**@asof.com> spoke thus:

char buf[20];
buf[n]++ = ''A'';//buf[n]++ isn''t a lvalue Why buf[n]++ isn''t a lvalue? Is this according to C Standard or is there
some reason for this? If buf[n]++ isn''t a lvalue, then why the following code works? while(*a++=*b++){}



Because *a++ is the same as *(a++). a is a pointer and so it''s fine to
increment it. buf[n], however, is a character, which you cannot increment.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Stub wrote:

The following code gives error C2105: ''++'' needs l-value.

char buf[20];
buf[n]++ = ''A'';//buf[n]++ isn''t a lvalue

Why buf[n]++ isn''t a lvalue? Is this according to C Standard or is there
some reason for this?
Why would you think that these two options are mutually exclusive?
;-). Things in the C standard *are* there for a reason!

Most likely, however, the expression you want is:

buf[n++] = ''A'';

If buf[n]++ isn''t a lvalue, then why the following code works?

void func(char *a, char *b)
{
while(*a++=*b++){}
}



Because here the *pointers* are dereferenced (*a is an lvalue,
yes?), the assignment is performed (yielding the expression''s value)
and finally each pointer is incremented.

HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.


Christopher Benson-Manica wrote:

buf[n], however, is a character, which you cannot
increment.



Yes, you can.
#include <stdio.h>

int main(void)
{
char buf[] = "3.1415926";
int n = 0;
buf[n]++;
printf("%s\n", buf);
return 0;
}

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


这篇关于buf [n] ++是左值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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