473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

qsort of an array of pointers to structures

19
Hi,

I wish to sort an array of pointers to structures. There must be something I don't get.

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. typedef struct
  3. {
  4.    ...
  5.    time_t time;
  6. }struct_file;
  7.  
  8. main()
  9. {
  10.    struct_file ** tables;
  11.    ... 
  12.    qsort( tables,ntables, sizeof(struct_file), cmp_struct);
  13. }
  14.  
  15. int cmp_struct( const void  *p1, const void *p2 )
  16. {
  17.    const struct_file *st1=p1; 
  18.    const struct_file *st2=p2; 
  19.  
  20.    if ((st1->time - st2->time)>=0)
  21.       return -1;
  22.    else
  23.       return 1;
  24. }
Do you have any idea?

Regards
Sep 29 '07 #1
1 3932
Ganon11
3,652 Expert 2GB
It looks like the qsort function:

Expand|Select|Wrap|Line Numbers
  1. void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
is looking for a single pointer as its first argument, but you are passing tables, which is a double pointer of type struct_file. Try calling it with

Expand|Select|Wrap|Line Numbers
  1. qsort(*tables, ntables, /* rest of your function call looks good */);
Sep 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Steve | last post by:
can someone tell me how qsort function in <stdlib.h> is used (qsort(..........))? the function has a buffer, two void * parameters and the a pointer to a compare function. Thanks.
7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
9
by: yogeshmk | last post by:
I'm trying to write a program which sorts the strings entered by user. I run it as $./srt -sa dddd a ccc bb # -sa is options to the program s-string a-ascending and expect a bb ccc
17
by: arne.muller | last post by:
Hello, I just had to find out that a program that runs fine under linux and True64 alpha crashed with a segmentation fault under SunOS 5.8. I drilled down and it appears that if the ...
10
by: gauss010 | last post by:
Suppose I have an object A of type char. Each A is a buffer containing a string, and I want to sort the M strings of A using the strcmp function. The description of the qsort function says that I...
4
by: davidcollins001 | last post by:
Hi, I am trying to get more to grips with pointers in C so I am trying to make a program that reads a file, places the data in a struct then sorts it using qsort. I have just about got my head...
17
by: Ron Ford | last post by:
Is qsort an intrinsic for C? -- We must respect the other fellow's religion, but only in the sense and to the extent that we respect his theory that his wife is beautiful and his children smart....
61
by: Ron Ford | last post by:
K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in §4.10. I think I'm pretty close...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.