First of all I would like to say that I am a super newb at C, and would appreciate any help possible. I have to read a text file of words and sort them in lexicographical order (alpha order), I tried using the bubble sort and got this error on lines 52 and 53. Please help me ... Heres the exact error..

Error E2277 Dictionary.c 52: Lvalue required in function dsort
Error E2277 Dictionary.c 53: Lvalue required in function dsort

IF you can respond asap that would be great,!

#include <stdio.h>

void dsort(char *name, char *words);

int main() {


char name[15];
char words[20][20];

printf("Please Enter a Filename for the dictionary File:\n\n");
scanf("%s", &name);
dsort(name, words);


return 0;
}


void dsort(char *name, char *words) {

char *wds[20][20];
//char wds1[20];
char temp;
int pass;
int i;

FILE *input_file = fopen(name, "r");

fscanf(input_file, "%s%s%s%s%s%s", wds[0],wds[1],wds[2],wds[3],wds[4],wds[5]);

//*words = wds;

printf("The Words are:%s\n%s\n%s\n%s\n%s\n%s\n", wds[0],wds[1],wds[2],wds[3],wds[4],wds[5]);

fclose(input_file);

for (pass=0;pass<6;++pass)
	{
		for (i=0;i<6;++i)
			{
				if (wds[i] > wds[i+1])
					{
					temp = wds[i];
		[B]Line52[/B]    wds[i] = wds[i+1];
                [B]line 53[/B]    wds[i+1] = temp;
					}
			}
	}



printf("The NEW Sorted Words are:%s\n%s\n%s\n%s\n%s\n%s\n", wds[0],wds[1],wds[2],wds[3],wds[4],wds[5]);



}

Dani AI

Generated

Quick diagnosis: the compiler message "Lvalue required" means the left side of an assignment is not a modifiable object. In your original dsort code the thing you try to assign to (the wds[...] expressions) and the temporary you used (char temp) don’t match as assignable objects, so the compiler refuses the swap. The underlying cause is a storage/type mismatch: how the words are stored determines whether you can assign pointers or must copy bytes.

Two practical fixes (pick one):

  • Keep fixed-size slots for each word (one buffer per word). Compare their contents and swap by copying the bytes through a temporary buffer whose size matches a slot. The temporary must be a character buffer big enough for the longest word, not a single char.

  • Store each word as a pointer and allocate or duplicate the text for each slot; then swap pointers directly. Example of a pointer swap (safe only if each element really is a char *):

    char *tmp = wds[i];
    wds[i] = wds[j];
    wds[j] = tmp;

Extra tips: always check fopen and input return values, avoid unbounded %s scanf (use field width or fgets + parsing), and be careful with loop bounds to avoid wds[i+1] running past the last element. If you want a simpler, tested sorter, use the library qsort with a comparator that compares strings by content.

As pointed toward the same root cause, fixing the storage/assignment strategy (either copy into buffers or swap pointers) will eliminate the lvalue error.

Recommended Answers

All 6 Replies

1. That function is doomed to fail because line 23 declared a 3d array of pointers. If what yoiu really want is a 2d character array where each element has room for 20 characters then you need to drop the star and just declare it like this: char wds[20][20]; 2. The two loops in your bubble sort are coded wrong. Should be this:

for (pass=0;pass<5;++pass)
{
     for(i = pass+1; i < 6; ++i)
     {
        // code here
     }
}

3. you have to use strcmp() to compare the two strings. Your program at line 43 is mearly comparing two pointers, not what they are pointing to.

if( strcmp(wds[pass], wds[i]) > 0)
{
   // swap the strings
}

4. You can not swap the two strings like you are attempting to do on line 45 You have to use strcpy().

5. why do you have the second parameter to dsort() ? It is never used so you might as well delete it.

6. Moved this thread to the C board since its a C program, not c++

HI, I WANTED TO THANK YOU FOR YOUR RESPONSE, IT HELPED ALOT!! THANKS AGAIN REALLY APPRECIATE IT!

1. That function is doomed to fail because line 23 declared a 3d array of pointers. If what yoiu really want is a 2d character array where each element has room for 20 characters then you need to drop the star and just declare it like this: char wds[20][20]; 2. The two loops in your bubble sort are coded wrong. Should be this:

for (pass=0;pass<5;++pass)
{
     for(i = pass+1; i < 6; ++i)
     {
        // code here
     }
}

3. you have to use strcmp() to compare the two strings. Your program at line 43 is mearly comparing two pointers, not what they are pointing to.

if( strcmp(wds[pass], wds[i]) > 0)
{
   // swap the strings
}

4. You can not swap the two strings like you are attempting to do on line 45 You have to use strcpy().

5. why do you have the second parameter to dsort() ? It is never used so you might as well delete it.

6. Moved this thread to the C board since its a C program, not c++

I don't know much about your doubt,but one thing to say is that,you have used " %s " in the " scanf() " function and at the same time used " & ".I think we should not use " & " if we use " %s " in the function " scanf() ".

If my post is not an appropriate one,please reply me.

thank you
vinaychalluru

The scanf() function is defined correctly.

scanf basically is used to read input from stdin according to the format specified. The format can consist of control characters which are %d %s %f and so forth.

All control characters must be preceeded by a % in both printf and scanf.

The variable arguments to scanf are in the form of the address of the variable, you want to read the values into hence the '&'.

The same set of format specifiers are allowed for both printf and scanf, but the variable arguments in case of printf are the variables themselves, whereas in case of scanf are the address of those variables.

ex printf("%s",string)
scanf("%s",&string)

ex printf("%s",string)
scanf("%s",&string)

Those statements are the same because string is defined to be a character array and not a pointer The & address operator in front of string is optional because both produce a pointer to the first byte of the string. Both lines below produce identical results.

int main(int argc, char* argv[])
{
    char string[] = "Hello World";
    printf("%s\n", string);
    printf("%s\n", &string);

    return 0;
}

It only works because "pointer to whole array - which is &array " and "pointer to first element of array - which is just array " happen to have the same value. The value may be the same, but you're playing with fire with the types.

int main(int argc, char* argv[])
{
    char string[] = "Hello World";
    char *p = string;
    printf("%s\n", string);
    printf("%s\n", &string);

    printf("%s\n", p); /* still works */
    printf("%s\n", &p); /* couldn't be more wrong */

    return 0;
}

'gcc -Wall' will warn you if you use &string where string was the correct choice, even it it seems to work for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.