2

I am learning the C programming language and have come across the qsort() function that lets you sort members of an array.

According to the GNU C library reference manual, the qsort() function is defined as follows:

void qsort (void *array, size_t count, size_t size, comparison_fn_t compare)

http://www.gnu.org/software/libc/manual/html_node/Array-Sort-Function.html#Array-Sort-Function

My question is, since this function is defined as using a certain data type for the count parameter, specifically size_t, how come a lot of examples that are posted use int as the datatype? When using this function in actual code, should I explicitly define my arguments as being size_t before passing them into this function?

The example that is used in the GNU C library reference manual also uses an int as an argument and everything works as expected:

#include <stdio.h>
#include <stdlib.h>

struct critter
{
    const char *name;
    const char *species;
};

struct critter muppets[] =
{
    {"Kermit", "frog"},
    {"Piggy", "pig"},
    {"Gonzo", "whatever"},
    {"Fozzie", "bear"},
    {"Sam", "eagle"},
    {"Robin", "frog"},
    {"Animal", "animal"},
    {"Camilla", "chicken"},
    {"Sweetums", "monster"},
    {"Dr. Strangepork", "pig"},
    {"Link Hogthrob", "pig"},
    {"Zoot", "human"},
    {"Dr. Bunsen Honeydew", "human"},
    {"Beaker", "human"},
    {"Swedish Chef", "human"}
};

int count = sizeof (muppets) / sizeof (struct critter);

int critter_cmp (const void *v1, const void *v2)
{
    const struct critter *c1 = v1;
    const struct critter *c2 = v2;
    return strcmp (c1->name, c2->name);
}

int main() 
{        
    qsort (muppets, count, sizeof (struct critter), critter_cmp);  
    return 0;
}

However, I feel that count in this example should be defined as:

size_t count = sizeof (muppets) / sizeof (struct critter);

Am I overthinking this, any help would be appreciated. Thank you.

Adam Bak
  • 1,269
  • 12
  • 15
  • 1
    You're right - strictly speaking it should really be a `size_t`, but for most purposes an `int` will be "good enough". – Paul R Sep 28 '15 at 15:45
  • 1
    C11 draft standard: `6.5.2.2 Function calls, Section 7 If the expression that denotes the called function has a type that does include a prototype, the arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type.[...]`. As long as you `#include `, you're good either way. – EOF Sep 28 '15 at 15:45
  • "how come a lot of examples that are posted use `int` as the datatype"? They're posting examples of bad code. You can wind up with some nasty bugs using a signed `int` where an unsigned `size_t` is specified, especially in an LP64 data model where `int` is 32 bits and `size_t` is a 64-bit type. – Andrew Henle Sep 28 '15 at 15:56
  • As a rule of thumb: Always use the data type that suits *best* the problem's need(s). – alk Sep 28 '15 at 16:02
  • 2
    If `strlen()` for example returns `size_t` is just sloppy (at least) to assign its result to an `int` as well as for the `sizeof` operator. Those two cases are just two common examples. – alk Sep 28 '15 at 16:03
  • 3
    And *no*, you are *not* "*overthinking this*", but doing well!-) – alk Sep 28 '15 at 16:06
  • BTW: This `int count = sizeof (muppets) / sizeof (struct critter);` would nicest look like this: `const size_t count = sizeof muppets / sizeof *muppets;` – alk Sep 28 '15 at 16:08
  • 1
    Thank you for all the input and thank you for the encouragement @alk . I am marking this post as duplicate since a lot of good information is also available in size_t vs int in C++ and/or C – Adam Bak Sep 28 '15 at 16:25
  • 1
    @PaulR: Replacing "*most* by "certain" or "some" would be the educational wording, wouldn't it? ;-) – alk Sep 28 '15 at 16:32
  • @alk: yes, you're probably right - arrays with more than 2 billion elements are relatively uncommon, but that's no excuse for sloppy programming. – Paul R Sep 28 '15 at 17:27

0 Answers0