473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 en*********@gawab.com and as a follow-up
to this message.
Nov 14 '05 #1
13 4976

"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
Nov 14 '05 #2
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>>
Nov 14 '05 #3
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>>
Nov 14 '05 #4
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 en*********@gawab.com and as a follow-up
to this message.

Nov 14 '05 #5
In 'comp.lang.c', en*********@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 en*********@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/
Nov 14 '05 #6
In 'comp.lang.c', "Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
int (*x)[10];
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".
(*x)=(int *) malloc( 30 * sizeof (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/
Nov 14 '05 #7
Emmanuel Delahaye <em**********@noos.fr> wrote in message news:<Xn***************************@212.27.42.73>. ..
In 'comp.lang.c', en*********@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.

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


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.
Nov 14 '05 #8
In 'comp.lang.c', en*********@gawab.com (wessoo) wrote:
int (*x)[10] = malloc (30 * sizeof *x);
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/
Nov 14 '05 #9
Emmanuel Delahaye <em**********@noos.fr> wrote in message news:<Xn***************************@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.
Nov 14 '05 #10
In 'comp.lang.c', en*********@gawab.com (wessoo) wrote:
But why ,C code are supposed to compile on c++ compilers(For
compatibility between c and c++).Am I wrong?


C code is just another a text file. There is nothing that prevents any text
file to be swallowed by a C++ compiler (well, I guess). Is it valid or not is
another story. C and C++ being different languages, chances are that C code
will not be compiled by a C++ compiler. Even if it was compiled, it doesn't
mean that the behavour in C++ will be the same than in C. In other words,
don't do that. Desipte urban legends, C and C++ are not compatible.

I personnally add this on my C sources:

#ifdef __cplusplus
#error This source file is not C++ but rather C. Please use a C-compiler
#endif

If you are curious (Mostly in French, sorry):

http://mapage.noos.fr/emdel/clib.htm

--
-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/
Nov 14 '05 #11
wessoo wrote:

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?


C is not C++. The languages are sufficiently similar that the C++
compiler can be made to compile C, iff _it knows about it_.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #12
wessoo <en*********@gawab.com> scribbled the following:
Emmanuel Delahaye <em**********@noos.fr> wrote in message news:<Xn***************************@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.


Try to compile this code on a C++ compiler:

int main(void) {
int new;
}

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Nothing lasts forever - so why not destroy it now?"
- Quake
Nov 14 '05 #13
wessoo wrote:
Emmanuel Delahaye <em**********@noos.fr> wrote in message news:<Xn***************************@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?


You're wrong. C++ compilers compile C++.

Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Marina | last post by:
Hi, if anyone can help on this, please! This is driving me crazy. I have a user control hierarchy several levels deep that derive for UserControl (windows). The class that is at the top level,...
2
by: WillWeGetOurFreedomBack | last post by:
I have a user who is unable to start an A2K app. This app runs fine everywhere else. The error he get is: "Required DLL: wwint32v.dll not found" I can find no reference to this file at...
1
by: paul reed | last post by:
Hello, I am having some weird behavior between two machines...one which is running the 1.1 framework and one which is running 1.0. After opening a child form from a parent...I update the...
4
by: Kevin Phifer | last post by:
Ok, before anyone freaks out, I have a solution I need to create that gathers content from maybe different places. Each one can return a <form> in the html, so its the classic can't have more than...
8
by: John Nagle | last post by:
Here's a wierd problem: I have a little test case for M2Crypto, which just opens up SSL connections to web servers and reads their certificates. This works fine. But if I execute ...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
8
by: Tony Winslow | last post by:
Hi, I'm wondering why the following code works just fine: #include <stdio.h> int main() { (******printf)("Hello!\n");
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.