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]);



}

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.