user1681673
user1681673

Reputation: 368

Writing a sorting function to sort an array of pointers, pointing to structures

I'm writing a function to sort an array of pointers, pointing to structures, based on the value of a zip code. I found a sort function online (it's my first time writing a sort function) and thought I would play around with it and see what happens and I keep getting the error "Array type 'char[7] is not assignable' and I'm not sure why. Any ideas?

Thank you.

struct personCatalog {
char name[50];
char address[50];
char cityState[50];
char zipCode[7];
} ;

#include <stdio.h>
#include "header.h"
#include <stdlib.h>
#include <string.h>

void bubble_sort(struct personCatalog *arrayOfPointers[]){
    int num1 = 0;

    while (arrayOfPointers[num1] != NULL) {
    atoi(arrayOfPointers[num1++]->zipCode);
    }

        int progress = 0;

        do {
            int i;
            progress = 0;
            for (i = 0; i < num1 - 2; ++i) {
                if (arrayOfPointers[i]->zipCode > arrayOfPointers[i + 1]->zipCode) {
                    struct personCatalog  temp = *arrayOfPointers[i];
                    arrayOfPointers[i] = arrayOfPointers[i + 1];
                    arrayOfPointers[i + 1] = &temp;

                    progress = 1;
                }
            }
        } while (progress);
    }

Error I'm receiving

Upvotes: 0

Views: 1288

Answers (3)

wildplasser
wildplasser

Reputation: 44250

The array consists of pointers. So the temp thing you are using to swap them should also be a pointer.

    for (i = 0; i < num1 - 1; ++i) {
            if ( strcmp( arrayOfPointers[i]->zipCode
                       , arrayOfPointers[i + 1]->zipCode
                       ) > 1) {
                struct personCatalog  *temp ;
                temp = arrayOfPointers[i];
                arrayOfPointers[i] = arrayOfPointers[i + 1];
                arrayOfPointers[i + 1] = temp;

                progress = 1;
            }
        }

Also, the loop condition i < num1-2 was wrong. Should be num1 -1, IMO.

Update: it appears zipcode is a textstring, so I replaced ">" by "strcmp()"

Upvotes: 0

Ricky Stewart
Ricky Stewart

Reputation: 1142

There are a couple problems with this code, but you're getting this error because you're trying to assign values to arrays in the highlighted lines; that's why it's complaining about assignment to array type char[7]. This doesn't make much sense, since you can't change the location of an array. You can either swap the bytes with a call to memcpy, or, perhaps more idiomatically, change the definition of struct personCatalog such that zipcode is a true char * rather than a char array.

Upvotes: 1

rutgersmike
rutgersmike

Reputation: 1213

The C language doesn't inherently know how to assign 7 chars to 7 other chars. It only allows you to assign one primitive type to another at a time:

zipCode[0] = temp[0]
zipCode[1] = temp[1];
// etc.

To copy arrays in C which are contiguous, like zipCode, you can use memcpy:

memcpy(zipCode, temp, 7);

Also, it's possible that I misread your intent on my tiny screen, but you also shouldn't assign a struct pointer to zipCode either.

Upvotes: 1

Related Questions