Sorting Array Error - left operand must be L-value 
Author Message
 Sorting Array Error - left operand must be L-value

I keep getting these errors with this problem.  I've tried every possible
combination of
"&" and without but still get the errors.  Also, every book that I have
lists the sorting
exchange the same way.  If I change to  == the program doesn't properly
sort.  This should sort the strings in alphabetical order.  Can anyone
help?
Thanks,
William Oliveri

#include <stdio.h>
#include <string.h>
#define MAX 8
#define LEN 5
int in, out;
char temp[MAX];
void main (void){
char list [MAX][LEN]={"Make","Cake","Date","Rake","Bait","Fake","Rate"};

for (out=0; out<MAX-1; out++)
        for (in=out+1; in<MAX; in++)
                if (strcmp(list[out],list[in])>0){
                        temp = list[in];
                        list[in] = list[out];
                        list[out] = temp;
                }

for (out=0; out<MAX; out++)
        printf("Name %d: %s\n", out+1, list[out]);

Quote:
}



Wed, 15 Dec 1999 03:00:00 GMT  
 Sorting Array Error - left operand must be L-value


Quote:
> I keep getting these errors with this problem.  I've tried every possible
> combination of
> "&" and without but still get the errors.  Also, every book that I have
> lists the sorting
> exchange the same way.  If I change to  == the program doesn't properly
> sort.  This should sort the strings in alphabetical order.  Can anyone
> help?

You cannot use the = operator on strings. You must use strcpy(). But you can
assign pointers. So, here's your code with a simple change:

Quote:
> #include <stdio.h>
> #include <string.h>
> #define MAX 8
> #define LEN 5
> int in, out;
> char *temp;    /* Change to pointer */
> void main (void){

/* Change to array of pointers */

Quote:
> char list *[MAX]={"Make","Cake","Date","Rake","Bait","Fake","Rate"};

> for (out=0; out<MAX-1; out++)
>    for (in=out+1; in<MAX; in++)
>            if (strcmp(list[out],list[in])>0){
>                    temp = list[in];
>                    list[in] = list[out];
>                    list[out] = temp;
>            }

> for (out=0; out<MAX; out++)
>    printf("Name %d: %s\n", out+1, list[out]);

> }

You could redo your swap using strcpy() on each line, but that's a waste of
time. Pointers are much more effective.


Thu, 16 Dec 1999 03:00:00 GMT  
 Sorting Array Error - left operand must be L-value

Quote:

> I keep getting these errors with this problem.  I've tried every possible
> combination of
> "&" and without but still get the errors.  Also, every book that I have
> lists the sorting
> exchange the same way.  If I change to  == the program doesn't properly
> sort.  This should sort the strings in alphabetical order.  Can anyone
> help?

> #include <stdio.h>
> #include <string.h>
> #define MAX 8
> #define LEN 5

Hi "bill oliveri",

Quote:
> int in, out;
> char temp[MAX];

            ^^^
The correct dimension corresponding to the logic of your program would
be "LEN".
And BTW, why did you choose global variables ?

Quote:
> void main (void){

The only correct return type for "main" is "int", change this to:
  int main( void )

Quote:
> char list [MAX][LEN]={"Make","Cake","Date","Rake","Bait","Fake","Rate"};

> for (out=0; out<MAX-1; out++)
>         for (in=out+1; in<MAX; in++)
>                 if (strcmp(list[out],list[in])>0){
>                         temp = list[in];
>                         list[in] = list[out];
>                         list[out] = temp;

The three assignments above are the reason for the error: in C you can't
assign an array (of characters in this case) to another array. You'll
either have to use a function for it, or do it element by element.

For character arrays there is the special "strcpy()" function which
copies
one character array into another up to and including the terminating
'\0'
character. More generally , there is a function called "memcpy()" to
copy one block of memory to another block of memory.

Examples:
  strcpy( temp, list[in] );
  strcpy( list[in], list[out] );
  strcpy( list[out], temp );

Quote:
> for (out=0; out<MAX; out++)
>         printf("Name %d: %s\n", out+1, list[out]);

Insert a "return EXIT_SUCCESS;" here for "int main".

Quote:
> }

Stephan
(initiator of the campaign against grumpiness in c.l.c)


Fri, 17 Dec 1999 03:00:00 GMT  
 Sorting Array Error - left operand must be L-value

Quote:

>The three assignments above are the reason for the error: in C you can't
>assign an array (of characters in this case) to another array. You'll
>either have to use a function for it, or do it element by element.

>For character arrays there is the special "strcpy()" function which
>copies
>one character array into another up to and including the terminating
>'\0'
>character. More generally , there is a function called "memcpy()" to
>copy one block of memory to another block of memory.

To be specific you use memcpy for general character arrays, strcpy() if you
want to copy a string.

--
-----------------------------------------


-----------------------------------------



Fri, 17 Dec 1999 03:00:00 GMT  
 Sorting Array Error - left operand must be L-value


Quote:

>>character. More generally , there is a function called "memcpy()" to
>>copy one block of memory to another block of memory.

>To be specific you use memcpy for general character arrays, strcpy() if you
>want to copy a string.

To be even more specific, memcpy is useful for manipulating arrays  of
character type and other objects treated as arrays of character
type. After all, the first two arguments of memcpy and the return
value are prototyped as void * and not char *.

Al Bowers                                
Tampa, FL

http://www.gate.net/~abowers/index.html



Sat, 18 Dec 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. gcc 2.7.2 warning: left-hand operand of comma expression has no effect

2. error C2106: '=' : left operand must be l-value

3. I am leaving!

4. expression return value / lvalue

5. Inline C Operand Type Error

6. error [130] incompatible operands when using constants

7. error C2443: operand size conflict

8. operands of : have incompatible types error

9. operands of : have incompatible types error message

10. Huh?? error C2659: '=' : overloaded function as left operand in vc7.1 beta

11. Array delarator an lvalue?

12. error C2659: '=' : overloaded function as left operand

 

 
Powered by phpBB® Forum Software