Bifalgorithm post · Computer science one · Comparator Functions

How can comparator functions order Student structures?

How can comparator functions order Student structures? (Computer science one · Comparator Functions) — "A comparator function is designed with certain expectat…

Ideas in this flowchart

  • Comparator functions order values through generic pointers.
  • The caller is expected to provide valid pointers to values of the expected type.
  • Violating these expectations causes unintended or undefined behavior.
  • Passing double pointers to cmpInt() makes the code compare the upper 32 bits of the double values.
  • Incorrect pointer types are the caller’s responsibility, as with many standard-library functions.
  • Passing non-null-terminated strings to most string functions is undefined behavior.
  • Comparator functions can order Student structures by one or more components.
  • studentByNameCmp() orders students alphabetically by last name and then first name.
  • The comparator casts the generic pointers to const Student pointers and compares the last names with strcmp().
  • When the last names are equal, the comparator compares the first names.
  • studentByNameCmpDesc() reuses the name comparator by reversing its arguments.
  • studentIdCmp() orders students by their nuid values in ascending numerical order.
  • The ID comparator casts the arguments to Student pointers and compares their nuid fields.
  • If the first nuid is smaller, the comparator returns -1.
  • If the two nuid values are equal, the comparator returns 0.
Feed/

How can comparator functions order Student structures?

No cover

Channel

Computer science one

1 page

"A comparator function is designed with certain expectations, namely that a user will always pass it valid pointers to values of the expected type. If two double pointers are passed to cmpInt(), the code will not work as intended because it will end up comparing the upper 32 bits of the double values, which leads to unintended behavior. If a user violates these expectations, it is their fault, not ours. This is no different from many other functions in the standard library; passing non-null-terminated strings to most string functions, for example, is undefined behavior.\n\nComparator functions can order Student structures based on one or more of their components. To order students by last name and then first name in alphabetic order, the comparator casts the generic pointers to const Student pointers, compares the last names with strcmp(), and compares the first names if the last names are equal:\n\nint studentByNameCmp(const void *s1, const void *s2) {\n\nconst Student *a = (const Student *)s1;\n\nconst Student *b = (const Student *)s2;\n\nint result = strcmp(a->lastName, b->lastName);\n\nif(result == 0) {\n\nreturn strcmp(a->firstName, b->firstName);\n\n} else {\n\nreturn result;\n\n}\n\n}\n\nA reverse alphabetic ordering can reuse this comparator by flipping the arguments:\n\nint studentByNameCmpDesc(const void *s1, const void *s2) {\n\nreturn studentByNameCmp(s2, s1);\n\n}\n\nStudents can also be ordered by id in ascending numerical order. The comparator casts the arguments to Student pointers and compares their nuid fields:\n\nint studentIdCmp(const void *s1, const void *s2) {\n\nconst Student *a = (const Student *)s1;\n\nconst Student *b = (const Student *)s2;\n\nif(a->nuid < b->nuid) {\n\nreturn -1;\n\n} else if(a->nuid == b->nuid) {\n\nreturn 0;\n\n}"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.