473,800 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

qsort of an array of pointers to structures

19 New Member
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 3961
Ganon11
3,652 Recognized Expert Specialist
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
3866
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
7455
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
2817
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
4456
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
1836
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 sun qsort gets a vector in which all elements are equivalent, my comparsion function raises SEGSEGV. The problem seems to be that all elements are the same sun qsort always passes NULL as the first element to the comparsion function....
10
2254
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 must pass a pointer to the first object of the array to be sorted. This leads me to write the following: char A; int cmp(const void *a, const void *b) { int v = strcmp(*(char (*))a, *(char (*))b); if (v < 0) return -1;
4
3892
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 around forming the array of pointers to structs but I can't seem to get it to qsort at all. I have looked at some other code that I have found but I can't seem to figure it out. I need help with the details of the "compare_keyword" function, that is...
17
235
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. 5 H. L. Mencken
61
5861
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 with this: void qsort(int v, int left, int right) { int i, last;
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.