Lvalue Required(wierd)...

W

wessoo

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[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[j],x[j]);

}

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

MANY THANKS,
KIND REGARDS
 
I

Ian Collins

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[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[j],x[j]);


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

wessoo

Ian said:
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[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[j],x[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,
 
A

Angel

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 = malloc( 30 * sizeof (int) );
 
B

Ben Bacarisse

wessoo said:
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[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[j],x[j]);

}


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

Keith Thompson

wessoo said:
* * 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.
 
I

Ian Collins

Ian said:
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[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[j],x[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).
 
K

Keith Thompson

wessoo said:
Ian said:
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[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[j],x[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.
 
W

wessoo

Ian said:
Ian said:
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[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[j],x[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[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[j], x[j]);
}
}
}
}
return 0;
}


Kind Regards
 
K

Keith Thompson

wessoo said:
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] *'

Then you're probably using a C++ compiler rather than a C compiler.
There's probably an option to tell your compiler to behave as a C
compiler.

In C, void* may be implicitly converted to any object pointer type;
casting the result of malloc() is unnecessary, and can hide errors.
(This is covered in the FAQ, <http://c-faq.com/>, but I'm having trouble
getting it to load at the moment.)

[...]
printf ("The index is %d.%d at address %d having value
%d\n"
,i , j, &x[j], x[j]);

[...]

When posting to Usenet, it's best to keep your lines short (72 columns
or less) so line-wrapping doesn't mess up your code like this.

One problem here: "%d" expects an int argument, but &x[j] is a
pointer. The format for printing pointer values is "%p", and it expects
a void* argument. There's no implicit conversion in this context, so
you should cast the argument to void*:

printf ("The index is %d.%d at address %p having value %d\n",
i, j, (void*)&x[j], x[j]);

"%p" will display the pointer value in some implementation-defined
format (hexadecimal is the most common).
 
W

wessoo

Keith said:
wessoo said:
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] *'

Then you're probably using a C++ compiler rather than a C compiler.
There's probably an option to tell your compiler to behave as a C
compiler.

In C, void* may be implicitly converted to any object pointer type;
casting the result of malloc() is unnecessary, and can hide errors.
(This is covered in the FAQ, <http://c-faq.com/>, but I'm having trouble
getting it to load at the moment.)

[...]
printf ("The index is %d.%d at address %d having value
%d\n"
,i , j, &x[j], x[j]);

[...]

When posting to Usenet, it's best to keep your lines short (72 columns
or less) so line-wrapping doesn't mess up your code like this.

One problem here: "%d" expects an int argument, but &x[j] is a
pointer. The format for printing pointer values is "%p", and it expects
a void* argument. There's no implicit conversion in this context, so
you should cast the argument to void*:

printf ("The index is %d.%d at address %p having value %d\n",
i, j, (void*)&x[j], x[j]);

"%p" will display the pointer value in some implementation-defined
format (hexadecimal is the most common).


Kieth - many thank yous for your informations. Is there rule for when
void* can be converted and can not be converted onto other pointer types?

Kind Regards
 
J

John Bode

Hi All.

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

An lvalue is an expression that refers to a block of memory in such a
way that the memory may be read or modified (modifiable lvalue). The
target of an assignment must be a modifiable lvalue.
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) );

First problem: the expression "*x" has type "10-element array of int",
and array expressions are not modifiable lvalues and cannot be the
target of an assignment.

Second problem: why are you declaring x to point to a 10-element array
of int, and then trying to allocate space to store 30 ints?

Assuming you want to allocate a 10-element array of int, you would
write

x = malloc(sizeof *x); // note no * on LHS, no cast

Assuming you want to allocate an 3-element array of 10-element arrays
of int, you would write

x = malloc(sizeof *x * 3); // effectively the same as "int x[3]
[10];"
 for(int i=0;i<5;i++)
    for(int j=0;j<6;j++)
       x[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[j],x[j]);


The scopes of i and j are limited to the first loops *only*; they
effectively go away after the outer loop finishes executing. You will
need to redeclare them in the second pair of loops.
 
S

Shao Miller

pointer. The format for printing pointer values is "%p", and it expects
a void* argument. There's no implicit conversion in this context, so
you should cast the argument to void*:

printf ("The index is %d.%d at address %p having value %d\n",
i, j, (void*)&x[j], x[j]);

"%p" will display the pointer value in some implementation-defined
format (hexadecimal is the most common).


Kieth - many thank yous for your informations. Is there rule for when
void* can be converted and can not be converted onto other pointer types?


It can always be converted, as far as I know. You can even do:

int main(void) {
void * vp = &vp;
return 0;
}

Even though 'vp' has type 'void *' and '&vp' has type 'void **'. This
might be confusing, but the point is that _any_ pointer type can be
converted to 'void *' (and back again). Please note: Only talking about
C here, not C++.

Since 'malloc()' returns a 'void *', you can always assign the result to
any pointer type.
 
B

Ben Bacarisse

Shao Miller said:
pointer. The format for printing pointer values is "%p", and it expects
a void* argument. There's no implicit conversion in this context, so
you should cast the argument to void*:

printf ("The index is %d.%d at address %p having value %d\n",
i, j, (void*)&x[j], x[j]);

"%p" will display the pointer value in some implementation-defined
format (hexadecimal is the most common).


Kieth - many thank yous for your informations. Is there rule for when
void* can be converted and can not be converted onto other pointer types?


It can always be converted, as far as I know.


Only to pointer to object and incomplete types -- not to function
pointers. Nothing to do with this example, but it's worth pointing
out.
You can even do:

int main(void) {
void * vp = &vp;
return 0;
}

Even though 'vp' has type 'void *' and '&vp' has type 'void **'. This
might be confusing, but the point is that _any_ pointer type can be
converted to 'void *' (and back again). Please note: Only talking
about C here, not C++.

Since 'malloc()' returns a 'void *', you can always assign the result
to any pointer type.
^ object
 
S

Shao Miller

Shao Miller said:
pointer. The format for printing pointer values is "%p", and it expects
a void* argument. There's no implicit conversion in this context, so
you should cast the argument to void*:

printf ("The index is %d.%d at address %p having value %d\n",
i, j, (void*)&x[j], x[j]);

"%p" will display the pointer value in some implementation-defined
format (hexadecimal is the most common).

Kieth - many thank yous for your informations. Is there rule for when
void* can be converted and can not be converted onto other pointer types?


It can always be converted, as far as I know.


Only to pointer to object and incomplete types -- not to function
pointers. Nothing to do with this example, but it's worth pointing
out.
You can even do:

int main(void) {
void * vp =&vp;
return 0;
}

Even though 'vp' has type 'void *' and '&vp' has type 'void **'. This
might be confusing, but the point is that _any_ pointer type can be
converted to 'void *' (and back again). Please note: Only talking
about C here, not C++.

Since 'malloc()' returns a 'void *', you can always assign the result
to any pointer type.
^ object


Absolutely; an excellent point. Oops and thanks. :) In fact, just
because of that, I use something like:

typedef void f_any(void);

and 'f_any *' instead of 'void *', when working with function pointers
in the sort of context where one might use 'void *' for object pointer
types. (See "cat" and "dog" example in "RE: Extending unions and ABI?"
thread.)
 
C

Chad

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

Did this code fall through a time warp from 25 years ago? :(


Maybe I'm wrong on this one, but it looks like the OP isn't making a
distinction between an array and the elements in that array. I know
these are two different things because I have an old comp.lang.c post
where Ben Pfaff actually explains the differences to me.

Chad
 
B

blmblm

Hi All.

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

An lvalue is an expression that refers to a block of memory in such a
way that the memory may be read or modified (modifiable lvalue). The
target of an assignment must be a modifiable lvalue.
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) );

First problem: the expression "*x" has type "10-element array of int",
and array expressions are not modifiable lvalues and cannot be the
target of an assignment.

Second problem: why are you declaring x to point to a 10-element array
of int, and then trying to allocate space to store 30 ints?

Assuming you want to allocate a 10-element array of int, you would
write

x = malloc(sizeof *x); // note no * on LHS, no cast

Assuming you want to allocate an 3-element array of 10-element arrays
of int, you would write

x = malloc(sizeof *x * 3); // effectively the same as "int x[3]
[10];"


But then you have three uninitialized pointers, don't you? which
of course is fixable but ....


The question I have for the OP, though, is this: What's that "10"
about? You seem to want a 2D array with 5 rows and 6 columns. How
does 10 come into things?

for(int i=0;i<5;i++)
for(int j=0;j<6;j++)
x[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[j],x[j]);


The scopes of i and j are limited to the first loops *only*; they
effectively go away after the outer loop finishes executing. You will
need to redeclare them in the second pair of loops.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top