Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How would I compare two char arrays in an array of structs

Posted on 2007-12-02
12
Medium Priority
?
850 Views
Last Modified: 2012-08-13
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!
0
Comment
Question by:pacman32689
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 13

Expert Comment

by:josgood
ID: 20392236
A struct is the same as a class, except that a struct's data is public by default.  So a struct can contain methods.

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.
0
 

Author Comment

by:pacman32689
ID: 20392237
here was my initial attempt
if( contact[i+1].name.compare(contact.name[i]) == -1)

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392241
strcmp() will do the job, but the sorting algorithm is another story. I will elaborate in a moment.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 400 total points
ID: 20392245
You can use the STL sort :

        http://www.cplusplus.com/reference/algorithm/sort.html

Something like :


#include <algorithm>
#include <string.h>
 
using namespace std;
 
struct MyStruct {
  // ...
  char name[16];
};
 
bool myStructCompare(MyStruct a, MyStruct b) {
  return (strcmp(a.name, b.name) < 0);
}
 
int main(void) {
  MyStruct array[10];
 
  // fill the array ...
 
  sort(&(array[0]), &(array[9]), myStructCompare);
 
  return 0;
}

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392247
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(arrayOfStruct)/sizeof(structType),sizeof(structType),comparefn);
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392258
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(contact)/sizeof(struct contactParameter),sizeof(struct contactParameter),comparefn);

notice my solution uses directly the char arrays (name), and doesn't require STL functions.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392267
>> 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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392274
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.
0
 

Author Comment

by:pacman32689
ID: 20392279
Would this not work?
 strcmp(contact.name[i],contact.name[i+1])

Open in new window

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 600 total points
ID: 20392284
I think you mean:
strcmp(contact[i].name,contact[i+1].name)
is it?
0
 

Author Comment

by:pacman32689
ID: 20392287
Yes, thanks so much, it compiled!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392289
>> 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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question