Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Lvalue Required(wierd)...
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 eng__wessam@gawab.com and as a follow-up
to this message.

Report this thread to moderator Post Follow-up to this message
Old Post
wessoo
06-26-04 12:07 AM


Re: Lvalue Required(wierd)...
"wessoo" <eng__wessam@gawab.com> wrote in message
news:b266e629.0406231511.75e92d1e@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

>  (*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



Report this thread to moderator Post Follow-up to this message
Old Post
Allan Bruce
06-26-04 12:07 AM


Re: Lvalue Required(wierd)...
On 23 Jun 2004 16:11:40 -0700, eng__wessam@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>>

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Schwarz
06-26-04 12:07 AM


Re: Lvalue Required(wierd)...
On Thu, 24 Jun 2004 00:14:54 +0100, "Allan Bruce"
<allanmb@TAKEAWAYf2s.com> wrote:

>
>"wessoo" <eng__wessam@gawab.com> wrote in message
>news:b266e629.0406231511.75e92d1e@posting.google.com... 
>
>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];

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

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Schwarz
06-26-04 12:07 AM


Re: Lvalue Required(wierd)...
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.

And you should be getting a _lot_ more diagnostics.  Turn your
diagnostics back on.  It is generally a good idea to have diagnostics
turned up.  In the case of someone who hasn't a clue what they are
doing, which is obviously true for you, it is insane to not have
diagnostics turned up.  Rather than comment on this hopeless code line
by line, here is an example which at least compiles that you might
compare your code to:

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

int main()
{
int (*x)[10];
int i, j;
x = malloc(5 * sizeof *x);

for (i = 0; i < 5; i++)
for (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 %p having value %d\n",
i, j, (void *) &x[i][j], x[i][j]);
return 0;
}





>
> 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 eng__wessam@gawab.com and as a follow-up
> to this message.

Report this thread to moderator Post Follow-up to this message
Old Post
Martin Ambuhl
06-26-04 12:07 AM


Re: Lvalue Required(wierd)...
In 'comp.lang.c', eng__wessam@gawab.com (wessoo) wrote:

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

It means that you try to write to somethinf that is not a left value of an
expression, like an array for example.

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

*My* Borland C is more severe (It's configured in ANSI source, display-all)

Compiling MAIN.C:
Error MAIN.C 2: main must have a return type of int
Warning MAIN.C 3: Call to function 'clrscr' with no prototype
Error MAIN.C 4: Declaration is not allowed here
Warning MAIN.C 5: Call to function 'malloc' with no prototype
Error MAIN.C 5: Lvalue required
Error MAIN.C 6: Expression syntax
Error MAIN.C 6: Undefined symbol 'i'
Error MAIN.C 6: Statement missing ;
Error MAIN.C 7: Undefined symbol 'j'
Error MAIN.C 7: Statement missing ;
Warning MAIN.C 13: Call to function 'printf' with no prototype
Warning MAIN.C 14: 'x' is declared but never used

>
> void main()

int main (void)

> {
>  clrscr();

Non standard and probably useless.

>  int (*x)[10];

This is the definition of a pointer to an array of 10 int.

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

So *x actually *is* an array. Writing to an array is an non-sense. You want
to update the pointer. Just drop the x.

BTW, also drop the cast (it's wrong and uselss) but include <stdlib.h>. Also
,
the expression for the size is suspicious. I guess you want a 2D array of 30
x 10 int. Hence the correct expression should be:

int (*x)[10] = malloc (30 * sizeof *x);

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

This construct ic valid in C99, but is not going to work with Turbo/Borland
C. Be sure that you are invoking the C compiler (The extension of the source
file must be .c). i and j must be defined separately.

>     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 correct formatter for an address is "%p" in conjunction with the (void*)
cast.

main() returning an int:

return 0;

> }
>
> The compiler is Turbo C for DOS(The same problem with Borland C++ for
> Dos too).
>
> Please e-mail your answers to eng__wessam@gawab.com and as a follow-up
> to this message.

No. This is a public forum. The answer is public too for the benefit of
everyone (Well, I hope so!). It also allow the peer review, because like any
human being, I do mistakes.

Here is a fixed version of you code.

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

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

if (x != NULL)
{
int     i;

for (i = 0; i < 5; i++)
{
int     j;
for (j = 0; j < 6; j++)
{
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 %p having value %d\n"
,i , j, (void *) &x[i][j], x[i][j]);
}
}
}
}
return 0;
}

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

Report this thread to moderator Post Follow-up to this message
Old Post
Emmanuel Delahaye
06-26-04 12:07 AM


Re: Lvalue Required(wierd)...
Xref: kermit comp.lang.c:389683

In 'comp.lang.c', "Allan Bruce" <allanmb@TAKEAWAYf2s.com> wrote:
 
>
> x is an array of 10 pointers

I'm not sure. I parse it "x is an uninitialized pointer to an array of 10
int".
 
>
> accessing *x is incorrect here.  You need to access one of the indices,

It's incorrect because *x is an array. An array is not a modifiable L-value.

> i.e. x[0]  now you are accessing the lvalue

I fail to see the difference between *x and x[0], x being a pointer.

I think you meant x.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

Report this thread to moderator Post Follow-up to this message
Old Post
Emmanuel Delahaye
06-26-04 12:07 AM


Re: Lvalue Required(wierd)...
Emmanuel Delahaye <emdelYOURBRA@noos.fr> wrote in message news:<Xns9512659226E81hsnoservern
et@212.27.42.73>...
> In 'comp.lang.c', eng__wessam@gawab.com (wessoo) wrote:
> 
>
> It means that you try to write to somethinf that is not a left value of an
> expression, like an array for example.
> 
> 
>
> This is the definition of a pointer to an array of 10 int.
> 
>
> So *x actually *is* an array. Writing to an array is an non-sense. You wan
t
> to update the pointer. Just drop the x.
>
> BTW, also drop the cast (it's wrong and uselss) but include <stdlib.h>. Al
so,
> the expression for the size is suspicious. I guess you want a 2D array of 
30
> x 10 int. Hence the correct expression should be:
>
> 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 of you code

#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++)
{
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 %p having value
%d\n"
,i , j, (void *) &x[i][j], x[i][j]);
}
}
}
}
return 0;
}

Please post your comments.

Report this thread to moderator Post Follow-up to this message
Old Post
wessoo
06-26-04 12:08 AM


Re: Lvalue Required(wierd)...
In 'comp.lang.c', eng__wessam@gawab.com (wessoo) wrote:
 
>
>  I tried this but I am geting the error message :-
>  Cannot convert 'void *' to 'int[10] *'

You are probably not invoking a C compiler. Check your project settings and
file extension (must be .c and not .C nor .cpp)

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

My code (main.c) was tested on Borland C 3.1. Trust me, If I was wrong, I
should have been DanPopped already (comunity joke)!

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

Report this thread to moderator Post Follow-up to this message
Old Post
Emmanuel Delahaye
06-26-04 12:08 AM


Re: Lvalue Required(wierd)...
Emmanuel Delahaye <emdelYOURBRA@noos.fr> wrote in message news:<Xns9513BF267
229Ahsnoservernet@212.27.42.68>...
I changed the extension and it actually worked.
Thank you.
But why ,C code are supposed to compile on c++ compilers(For
compatibility between c and c++).Am I wrong?
Thank you again.

Report this thread to moderator Post Follow-up to this message
Old Post
wessoo
06-26-04 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

C archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:48 PM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.