sorting a structure

mml0707 used Ask the Experts™
I've created a data file using an array of structures and now I'm trying to create a sort function that will sort the structure member char payTo[15]and print out the file in alphabetical order. I'm trying to use a bubble sort but I can't get it to work. I keep getting an error stating "error C2106: '=' : left operand must be l-value". I don't understand what I'm doing wrong. I've already created a search function that works fine but my sort function has me stumped !
Can anyone look at my code and give me an idea as to what I'm doing wrong ?
Here is what i have:

struct checkBook{
int check;
char payTo[15];
double amt;
char description[15];

//body of main I opened up the file for reading

void search_payTo(void)
char payTo[15];
int i, found;

printf("Enter pay to order of to search for: ");

found = 0;
for(i=0; i<top; i++)
if(!strcmp(payTo, cb[i].payTo)){
found = 1;
if(!found) printf("Not found\n");

void sort_payTo(void)
int i, j;
char temp[80];

printf("Sorted by Pay to the order of\n");

for(i=1; i<top; i++){
for(j=0; j<top-1; j++){
if(strcmp(cb[j].payTo, cb[j+1].payTo)>0)
temp = cb[j].payTo;
cb[j].payTo = cb[j+1].payTo;
cb[j+1] = temp;

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Could you post the complete code?
A quick scan of your code shows a problem in the sort_payTo() routine:

    char temp[80];


    temp = cb[j].payTo           /* 1 */
    cb[j].payTo = cb[j+1].payTo; /* 2 */
    cb[j+1] = temp;              /* 3 */

Each of the three lines have the same error: you are attempting to assign an array to an array. This is not legal C.

Details: the name of an array is not an lvalue and can not be on the left side of the assignment operator. On the right side, the array name is converted to a pointer to the first value of the array. So were the assignment to be allowed, you'd be copying the pointer value, not the array contents. Refer to your fave C text, Harbison & Steele, or such.

Either use strcpy() (strncpy() is better) or memcpy()


first of all .. how come u have functions defined in thw body of the main

declare a function like this

int compare(void *v1,void *v2){

return strcmp((struct checkBook*)v1->payTo,(struct checkBook*)v2->payTo);

and when u have ur members filled in the
cb array

call this in the main

qsort(cb,numOfRecords,sizeof(struct checkBook),compare);
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

>>>first of all .. how come u have functions defined in thw body of the main
sorry for this .. i meant how r u using the main body . where is it ?

I would suggest using a dynamic array of pointers to these structures. That way you'd only have to move around the pointers when sorting. Then there is a very useful function called qsort. Look it up and use it.

Yo! Fellow commenteers... The question is about a compile time error. It is not about how best to sort an array of structs.

Yeah, but you answered those problems already... I don't expect any credits for my comments here.

u cannot reassign array because array is nothing but a constant pointer..
and you cannot change the constant stuff once u initialise it.
Top Expert 2006

No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to ewest

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.


EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial