pacman32689
asked on
How would I compare two char arrays in an array of structs
Hi experts, quick general question. How would I go about comparing to char arrays from an array of structs, and sorting the structs in the array accordingly? (In this case alphabetical order).
I have tried compare(), but I get an error telling me there is an error for request of member of the struct I am trying to access.
Thanks for the help!
I have tried compare(), but I get an error telling me there is an error for request of member of the struct I am trying to access.
Thanks for the help!
ASKER
here was my initial attempt
if( contact[i+1].name.compare(contact.name[i]) == -1)
strcmp() will do the job, but the sorting algorithm is another story. I will elaborate in a moment.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
qsort() will be the shortest method. first define a comparison function, there you will use the strcmp() function:
#include <stdlib.h>
int comparefn(const void *a, consr void *b){
return strcmp((struct yourstruct *)a)->name, ((struct yourstruct *)b)->name);
}
then you can use qsort, using the new function as a callback:
qsort(arrayOfStruct,sizeof (arrayOfSt ruct)/size of(structT ype),sizeo f(structTy pe),compar efn);
#include <stdlib.h>
int comparefn(const void *a, consr void *b){
return strcmp((struct yourstruct *)a)->name, ((struct yourstruct *)b)->name);
}
then you can use qsort, using the new function as a callback:
qsort(arrayOfStruct,sizeof
using your code of previous questions, should be:
#include <stdlib.h>
int comparefn(const void *a, consr void *b){
return strcmp((struct yourstruct *)a)->name, ((struct yourstruct *)b)->name);
}
then you can use qsort, using the new function as a callback:
qsort(contact,sizeof(conta ct)/sizeof (struct contactParameter),sizeof(s truct contactParameter),comparef n);
notice my solution uses directly the char arrays (name), and doesn't require STL functions.
#include <stdlib.h>
int comparefn(const void *a, consr void *b){
return strcmp((struct yourstruct *)a)->name, ((struct yourstruct *)b)->name);
}
then you can use qsort, using the new function as a callback:
qsort(contact,sizeof(conta
notice my solution uses directly the char arrays (name), and doesn't require STL functions.
>> and doesn't require STL functions.
You say that like it's a bad thing. It's not ... STL is the standard template library, and is part of C++ ... not using it is passing up on a great tool.
You say that like it's a bad thing. It's not ... STL is the standard template library, and is part of C++ ... not using it is passing up on a great tool.
I would suggest to use STL functions, if the struct has STL objects, but it is not the case.
I always try to avoid mixing libraries as far as possible.
I always try to avoid mixing libraries as far as possible.
ASKER
Would this not work?
strcmp(contact.name[i],contact.name[i+1])
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, thanks so much, it compiled!
>> Would this not work?
If name is an array of strings, yes, but I suspect that name is a string (array of characters), so you just do :
strcmp(contact[i].name, contact[i+1].name)
to compare the name of contact i with the name of contact i + 1.
If name is an array of strings, yes, but I suspect that name is a string (array of characters), so you just do :
strcmp(contact[i].name, contact[i+1].name)
to compare the name of contact i with the name of contact i + 1.
I suggest a comparison method which is a member of the struct. The comparison method would take a char array and compare to the char array which is also a member of the struct.