wessoo
5/23/2011 8:12:00 PM
Ian Collins writes:
> On 05/23/11 09:23 AM, wessoo wrote:
>> Ian Collins writes:
>>> On 05/23/11 09:05 AM, 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();
>>>> int (*x)[10];
>>>> (*x)=(int *) malloc( 30 * sizeof (int) );
>>>
>>> You can't assign an array.
>>>> 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]);
>>>
>>> This couldn't have compiled, i and j haven't been declared in this
>>> scope.
>>
>> Look up Ian, i and j are declared in previous loop, it compiles quite
>> fine.
>
> Yes they are and that is the scope where they live. Outside of the loop
> they are undeclared. if you put your compiler in conforming mode (i.e.
> make it a C compiler), it will barf.
>
>> I am not assigning any array but I do allocate 2-d space for x (NB
>> 30=6*5)...
>
> Yes you are. (*x) is an array, an array can not appear on the left-hand
> side of an expression (be an lvalue in standards speak).
OK... thanks I see now. I have fix the code. One thing is confuse me: I
have tried
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
#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++) // NB: 30=6*5
{
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 %d having value
%d\n"
,i , j, &x[i][j], x[i][j]);
}
}
}
}
return 0;
}
Kind Regards