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. 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
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>>
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.
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/
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.
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/
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.
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!
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
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++. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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...
|
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...
|
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
...
|
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/...
|
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");
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |