需要左值(奇怪)...... [英] Lvalue Required(wierd)...

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

问题描述

大家好。


什么是Lvalue Required错误消息。

(这是什么意思?它是什么的缩写。)


我写了这个测试程序,我正在保留这条消息。


void main()

{

clrscr();

int(* x)[10];

(* x)=(int *)malloc(30 * sizeof(int ));

for(int i = 0; i< 5; i ++)

for(int j = 0; j< 6; j ++)

x [i] [j] = i * 10 + j;


for(i = 0; i< 5; i ++)

for( j = 0; j< 6; j ++)

printf("索引是%d%d,地址%d,有价值

%d \ n", i,j,& x [i] [j],x [i] [j]);

}


编译器是Turbo C for DOS(与Borland C ++相同的问题

Dos)。


请将您的答案通过电子邮件发送给 en ********* @ gawab.com 以及后续消息

来自此消息。

解决方案



wessoo <恩********* @ gawab.com>在留言中写道

news:b2 ************************** @ posting.google.c om ...

大家好。

什么是Lvalue Required错误信息。
(这是什么意思?它是什么的缩写。)

void main()
{
clrscr();
int(* x)[ 10];


x是10个指针的数组

(* x)=(int *)malloc(30 * sizeof(int));




访问* x在这里不正确。您需要访问其中一个索引,即

x [0]现在您正在访问左值

HTH

Allan


2004年6月23日16:11:40 -0700, en **** *****@gawab.com (wessoo)写道:

大家好。

什么是Lvalue Required错误消息。
(这是什么意思?它是某个东西的缩写。)

我写了这个测试程序,我正在保留这个消息。

void main()


int main(无效)

{
clrscr();


非标准不必要。如果你想让别人帮忙,即使他们使用不同的系统,也要给他们提供他们可以编译的代码。

int(* x)[10];


x是指向10 int数组的指针。

(* x)=(int *)malloc(30 * sizeof(int));


* x是x指向的类型。由于x指向一个数组,* x是类型数组的
。数组类型可能不会出现在

赋值的左侧。


因为x当前是未初始化的指针,所以你可能意味着

x = ...

但你还有其他问题:


不要从malloc投出回报。它几乎没有任何帮助,并且它会导致编译器在范围内抑制关于没有原型

的诊断。如果范围内没有原型,则调用malloc在C89上调用

未定义的行为(因为编译器需要假设

,malloc返回一个我们知道不真实的int )并且是C99中的一个

约束违规。


int *是分配给x或* x的错误类型。


30 * sizeof(int)可以工作,但它有误导性(有人可能会稍后

尝试将30改为35或其他非10的倍数。你

真的需要类似

x = malloc(3 * sizeof * x);

for(int i = 0; i< 5; i ++)
for(int j = 0; j< 6; j ++)
x [i] [j] = i * 10 + j;


x是指向数组的指针10 int。x [i]是那个

地址的第i个数组.x [i] [j]是第i个数组中的第j个int。你分配了

空间为30 int。当i> 2时,你通过

调用未分配的行为来访问你的分配范围之外的内存。

for(i = 0; i< 5; i ++)
对于(j = 0; j <6; j ++)
printf(索引是%d%d在地址%d处具有值
%d \ n,i,j和& X [i] [j],X [i] [j]);


%d不适合地址格式。使用%p并将

对应的参数转换为void *。

}

编译器是用于DOS的Turbo C(与Borland相同的问题) C ++ for
Dos)。




<<删除电子邮件的del>>


2004年6月24日星期四00:14:54 +0100,Allan Bruce

< al ***** @ TAKEAWAYf2s.com>写道:


wessoo <恩********* @ gawab.com>在消息中写道
新闻:b2 ************************** @ posting.google。 com ...

大家好。

什么是Lvalue Required错误消息。
(这是什么意思?它是什么的缩写。)

我写了这个测试程序,我正在保留这个消息。

void main()
{
clrscr();
int( * x)[10];
x是10个指针的数组




不,x是指向10 int数组的指针。你描述的类型将被编码为int * x [10];

(* x)=(int *)malloc(30 * sizeof( int));
访问* x在这里不正确。您需要访问其中一个索引,即
x [0]现在您正在访问左值


很难。 C语言要求* x和x [0]在

中相同。

HTH




很难。

<<删除电子邮件的del>>


Hi All.

What is The Lvalue Required error message.
(What does it mean?Is it an abbreviationof something.)

I wrote this test program and I am keeping geting this message.

void main()
{
clrscr();
int (*x)[10];
(*x)=(int *) malloc( 30 * sizeof (int) );
for(int i=0;i<5;i++)
for(int j=0;j<6;j++)
x[i][j]=i*10+j;

for(i=0;i<5;i++)
for(j=0;j<6;j++)
printf("The index is %d%d at address %d having value
%d\n",i,j,&x[i][j],x[i][j]);
}

The compiler is Turbo C for DOS(The same problem with Borland C++ for
Dos too).

Please e-mail your answers to en*********@gawab.com and as a follow-up
to this message.

解决方案


"wessoo" <en*********@gawab.com> wrote in message
news:b2**************************@posting.google.c om...

Hi All.

What is The Lvalue Required error message.
(What does it mean?Is it an abbreviationof something.)

I wrote this test program and I am keeping geting this message.

void main()
{
clrscr();
int (*x)[10];
x is an array of 10 pointers
(*x)=(int *) malloc( 30 * sizeof (int) );



accessing *x is incorrect here. You need to access one of the indices, i.e.
x[0] now you are accessing the lvalue
HTH
Allan


On 23 Jun 2004 16:11:40 -0700, en*********@gawab.com (wessoo) wrote:

Hi All.

What is The Lvalue Required error message.
(What does it mean?Is it an abbreviationof something.)

I wrote this test program and I am keeping geting this message.

void main()
int main(void)
{
clrscr();
Non-standard an unnecessary. If you want people to help, give them
code they can compile, even if they use a different system.
int (*x)[10];
x is a pointer to an array of 10 int.
(*x)=(int *) malloc( 30 * sizeof (int) );
*x is of the type that x points to. Since x points to an array, *x is
of type array. An array type may not appear on the left of an
assignment.

Since x is currently an uninitialized pointer, your probably meant
x = ...
but you also have other problems:

Don''t cast the return from malloc. It can hardly ever help and
it causes the compiler to suppress the diagnostic about no prototype
in scope. If there is no prototype in scope, calling malloc invokes
undefined behavior on C89 (because the compiler is required to assume
that malloc returns an int which we know to be untrue) and is a
constraint violation in C99.

int* is the wrong type to assign to x or *x.

30 * sizeof(int) can work but it is misleading (someone may later
try to change the 30 to 35 or some other non-multiple of 10. You
really want something like
x = malloc(3 * sizeof *x);
for(int i=0;i<5;i++)
for(int j=0;j<6;j++)
x[i][j]=i*10+j;
x is a pointer to an array of 10 int. x[i] is the i-th array at that
address. x[i][j] is the j-th int in the i-th array. You allocated
space for 30 int. When i>2, you invoke undefined behavior by
accessing memory outside the bounds of your allocation.

for(i=0;i<5;i++)
for(j=0;j<6;j++)
printf("The index is %d%d at address %d having value
%d\n",i,j,&x[i][j],x[i][j]);
%d is not a suitable format for an address. Use %p and cast the
corresponding argument to void*.
}

The compiler is Turbo C for DOS(The same problem with Borland C++ for
Dos too).



<<Remove the del for email>>


On Thu, 24 Jun 2004 00:14:54 +0100, "Allan Bruce"
<al*****@TAKEAWAYf2s.com> wrote:


"wessoo" <en*********@gawab.com> wrote in message
news:b2**************************@posting.google. com...

Hi All.

What is The Lvalue Required error message.
(What does it mean?Is it an abbreviationof something.)

I wrote this test program and I am keeping geting this message.

void main()
{
clrscr();
int (*x)[10];
x is an array of 10 pointers



No, x is a pointer to an array of 10 int. The type you describe would
be coded as int *x[10];

(*x)=(int *) malloc( 30 * sizeof (int) );
accessing *x is incorrect here. You need to access one of the indices, i.e.
x[0] now you are accessing the lvalue



Hardly. The C language requires *x and x[0] to be identical in
meaning.
HTH



Hardly.
<<Remove the del for email>>


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

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