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

memcpy(temp, arrayOfPointers[i], sizeof(struct personCatalog));the first argument is the culprit (in the other line the second, both times it'stemp). You should pass a pointer:&temp. But you'rememcpying the whole struct, you can have that easier,temp = *arrayOfPointers[i];, structs are assignable. In your original, you tried to assign totemp->zipCode(tempwas a pointer then). That was what caused the first error. If you only want to swap thezipCodes, you mustmemcpy(&(temp.zipCode), &(arrayOfpointers[i].zipCode), sizeof temp.zipCode);etc.tempis a local variable, so&tempbecomes a dangling pointer after theiffinishes. With astruct personCatalog temp;, you need to swap the pointed-to values, so*arrayOfPointers[i] = *arrayOfPointers[i+1];etc. But since you have an array of pointers, it's less work to just swap the pointers, so use a pointer as temporary variable,struct personCatalog *temp = arrayOfPointers[i]; ... arrayOfPointers[i+1] = temp;.