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);
}
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()"