[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Lvalue Required(wierd)...

wessoo

5/22/2011 9:06:00 PM

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).

MANY THANKS,
KIND REGARDS
20 Answers

Ian Collins

5/22/2011 9:19:00 PM

0

On 05/23/11 09:05 AM, 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();
> int (*x)[10];
> (*x)=(int *) malloc( 30 * sizeof (int) );

You can't assign an array.
> 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]);

This couldn't have compiled, i and j haven't been declared in this scope.

--
Ian Collins

wessoo

5/22/2011 9:20:00 PM

0

* * BUMP * *

There's anyone in this room??


wessoo writes:
> 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).
>
> MANY THANKS,
> KIND REGARDS

wessoo

5/22/2011 9:24:00 PM

0

Ian Collins writes:
> On 05/23/11 09:05 AM, 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();
>> int (*x)[10];
>> (*x)=(int *) malloc( 30 * sizeof (int) );
>
> You can't assign an array.
>> 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]);
>
> This couldn't have compiled, i and j haven't been declared in this
> scope.

Look up Ian, i and j are declared in previous loop, it compiles quite
fine.

I am not assigning any array but I do allocate 2-d space for x (NB
30=6*5)...

Regards,

Angel

5/22/2011 9:28:00 PM

0

On 2011-05-22, wessoo <wessoo@notmyrealemail.com> wrote:
> Hi All.
>
> What is The Lvalue Required error message.
> (What does it mean?Is it an abbreviationof something.)

An lvalue is the abbreviation of a left value, that is any expression
that results in a value that can legally be placed to the left of an
assignment operator. In other words, it is anything that you can
directly assign a value to.

Variables are lvalues, if they are not declared const. Pointer
derefences can be lvalues too. Constants can never be lvalues.

> I wrote this test program and I am keeping geting this message.
>
> void main()
> {
> clrscr();
> int (*x)[10];
> (*x)=(int *) malloc( 30 * sizeof (int) );

This whole thing is messed up. (*x) is an array of int with ten
elements, making x a pointer to an array with then elements. Then you
assign the result of malloc to this array. You can't assign to arrays
(except in initialization), only to the elements of arrays.

What you want is more something like this:

int *x[10];
for (size_t i = 0; i < 10; i++)
x[i] = malloc( 30 * sizeof (int) );


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

Ben Bacarisse

5/22/2011 9:30:00 PM

0

wessoo <wessoo@notmyrealemail.com> writes:

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

Roughly speaking, and "lvalue" is something that can be assigned to --
something that can go on the left side on the = operator (the "l" in
lvalue is from "left").

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

There are too many think wrong with program for it to be worth going
over them one by one. I think you need to find another way to learn.
Whatever course, book or tutorial you are using is leading you to
make a lot of errors.

> void main()
> {
> clrscr();
> int (*x)[10];
> (*x)=(int *) malloc( 30 * sizeof (int) );

This is the lvalue problem. x is a pointer to an array. *x is
therefore an array, and in C you can't assign to an array. I'd offer a
correction but it would have to be a guess.

x = malloc(3 * sizeof *x);

is one possibility.

> 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]);
>
> }

I count about 10 errors in as many lines. Please find some other source
of information about C.

--
Ben.

Keith Thompson

5/22/2011 9:36:00 PM

0

wessoo <wessoo@notmyrealemail.com> writes:
> * * BUMP * *
>
> There's anyone in this room??
[...]

This is a newsgroup, not a chat room. Not getting a reply after 19
minutes isn't a cause for concern. If nobody answered within a day or
so, it might indicate a problem.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ian Collins

5/22/2011 9:38:00 PM

0

On 05/23/11 09:23 AM, wessoo wrote:
> Ian Collins writes:
>> On 05/23/11 09:05 AM, 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();
>>> int (*x)[10];
>>> (*x)=(int *) malloc( 30 * sizeof (int) );
>>
>> You can't assign an array.
>>> 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]);
>>
>> This couldn't have compiled, i and j haven't been declared in this
>> scope.
>
> Look up Ian, i and j are declared in previous loop, it compiles quite
> fine.

Yes they are and that is the scope where they live. Outside of the loop
they are undeclared. if you put your compiler in conforming mode (i.e.
make it a C compiler), it will barf.

> I am not assigning any array but I do allocate 2-d space for x (NB
> 30=6*5)...

Yes you are. (*x) is an array, an array can not appear on the left-hand
side of an expression (be an lvalue in standards speak).

--
Ian Collins

ram

5/22/2011 10:11:00 PM

0

Angel <angel+news@spamcop.net> writes:
>An lvalue is the abbreviation of a left value

The word »lvalue« is derived from the phrase »left value«.

An lvalue is an expression with an object type or an
incomplete type other than void.

http://en.wikipedia.org/wiki/Use%E2%80%93mention_d...

(A modifiable lvalue is an lvalue that does not have array type.)

Keith Thompson

5/23/2011 2:03:00 AM

0

wessoo <wessoo@notmyrealemail.com> writes:
> Ian Collins writes:
>> On 05/23/11 09:05 AM, 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();
>>> int (*x)[10];
>>> (*x)=(int *) malloc( 30 * sizeof (int) );
>>
>> You can't assign an array.
>>> 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]);
>>
>> This couldn't have compiled, i and j haven't been declared in this
>> scope.
>
> Look up Ian, i and j are declared in previous loop, it compiles quite
> fine.

In some early (pre-standard) versions of C++, the scope of a
variable declared in a for loop header extended to the block
containing the loop. In modern C++, and in C99, the scope is
limited to the body of the loop. You're probably using an old C++
compiler. Since you're posting in comp.lang.c, you probably want
to be using a C compiler instead, preferably a fairly modern one
(older C compilers don't support declaraing variables in for loops).

> I am not assigning any array but I do allocate 2-d space for x (NB
> 30=6*5)...

Yes, you are assigning to an array, or rather you're attempting to.
That's what the compiler is complaining about.

Your declaration

int (*x)[10];

declares x as a pointer to an array of 10 ints. (Incidentally, it's
unlikely that this is really what you want, but I can't tell from your
program just what you do want.)

Since x is a pointer to an array, (*x) is an array. You have (*x) on
the left side of an assignment. You're trying to assign to an array.

We can help you modify your code so it compiles, but we can't help you
modify it to do what you want it to do without knowing what you want it
to do.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

wessoo

5/23/2011 8:12:00 PM

0

Ian Collins writes:

> On 05/23/11 09:23 AM, wessoo wrote:
>> Ian Collins writes:
>>> On 05/23/11 09:05 AM, 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();
>>>> int (*x)[10];
>>>> (*x)=(int *) malloc( 30 * sizeof (int) );
>>>
>>> You can't assign an array.
>>>> 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]);
>>>
>>> This couldn't have compiled, i and j haven't been declared in this
>>> scope.
>>
>> Look up Ian, i and j are declared in previous loop, it compiles quite
>> fine.
>
> Yes they are and that is the scope where they live. Outside of the loop
> they are undeclared. if you put your compiler in conforming mode (i.e.
> make it a C compiler), it will barf.
>
>> I am not assigning any array but I do allocate 2-d space for x (NB
>> 30=6*5)...
>
> Yes you are. (*x) is an array, an array can not appear on the left-hand
> side of an expression (be an lvalue in standards speak).

OK... thanks I see now. I have fix the code. One thing is confuse me: I
have tried
int (*x)[10] = malloc (30 * sizeof *x);

I tried this but I am geting the error message :-
Cannot convert 'void *' to 'int[10] *'

So the compiler can not do an implicit cast from 'void *' to a
pointer to an array.
We have to do the cast explicitly.
int (*)[10] = (int (*)[])malloc (30 * sizeof *x);
The rest of the code is ok.
Is that ok with C99
I compiled this in Turbo/Borland C and it worked just fine.
So Here is the refixed version

#include <stdlib.h>
#include <stdio.h>

int main ()
{
/* makes an array of 30 arrays of 10 int */
int (*x)[10] = (int (*)[])malloc (30 * sizeof *x);

if (x != NULL)
{
int i;

for (i = 0; i < 5; i++)
{
int j;
for (j = 0; j < 6; j++) // NB: 30=6*5
{
x[i][j] = i * 10 + j;
}
}

{
int i;
for (i = 0; i < 5; i++)
{
int j;
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]);
}
}
}
}
return 0;
}


Kind Regards