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
int main (void)
Non standard and probably useless.
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);
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;
}