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

Please e-mail your answers to (e-mail address removed) and as a follow-up
to this message.
 
A

Allan Bruce

wessoo 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()
{
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
 
B

Barry Schwarz

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[j]=i*10+j;


x is a pointer to an array of 10 int. x is the i-th array at that
address. x[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[j],x[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>>
 
B

Barry Schwarz

wessoo 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()
{
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.

Hardly.


<<Remove the del for email>>
 
M

Martin Ambuhl

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

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

Please e-mail your answers to (e-mail address removed) and as a follow-up
to this message.
 
E

Emmanuel Delahaye

In said:
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[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 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 (e-mail address removed) 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[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[j], x[j]);
}
}
}
}
return 0;
}
 
E

Emmanuel Delahaye

Allan Bruce said:
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".
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.
 
W

wessoo

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

Please post your comments.
 
E

Emmanuel Delahaye

In said:
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)!
 
W

wessoo

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

Emmanuel Delahaye

In said:
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
 
C

CBFalconer

wessoo said:
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_.
 
J

Joona I Palaste

wessoo said:
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;
}
 
M

Martin Ambuhl

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

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