Bifalgorithm post · Computer science one · Comparator Functions

How do you implement a comparator for integers in C?

How do you implement a comparator for integers in C? (Computer science one · Comparator Functions) — "To write a comparator function that orders integers in as…

Ideas in this flowchart

  • Implementación de comparadores para ordenar valores
  • Convertir los punteros genéricos al tipo específico que se compara
  • Comparador de enteros en orden ascendente
  • Comparador de enteros en orden descendente
  • Patrón general de implementación de comparadores
  • Un puntero void no puede desreferenciarse porque el tamaño del tipo no está definido
  • Los punteros int* o double* sí pueden desreferenciarse porque el compilador conoce el tamaño del tipo
  • Realizar una conversión explícita a const int*: const int *x = (const int *) a;
  • Conservar const para impedir que el comparador modifique las variables
  • Comparar los valores enteros desreferenciados
  • Si *x < *y, devolver un valor negativo
  • Si *x == *y, devolver cero
  • En caso contrario, devolver un valor positivo
  • Reutilizar el comparador ascendente invirtiendo sus argumentos
  • cmpIntDesc(const void *a, const void *b) devuelve cmpInt(b, a)
  • En estructuras, comparar el estado del tipo mediante uno o más componentes
Feed/

How do you implement a comparator for integers in C?

No cover

Channel

Computer science one

1 page

"To write a comparator function that orders integers in ascending order, the generic pointers must first be converted into pointers to the specific type being compared. In general, a void pointer cannot be dereferenced because the number of bytes a void type takes is undefined. Dereferencing an int * or double * is possible because the compiler knows how many bytes each type takes. The first step is therefore to make the pointers int pointers by doing an explicit type cast:\n\nconst int *x = (const int *) a;\n\nconst int *y = (const int *) b;\n\nThe const keyword is preserved to ensure that the comparator does not make changes to the variables. The resulting comparator compares the dereferenced integer values and returns a negative value, zero, or a positive value:\n\nint cmpInt(const void *a, const void *b) {\n\nconst int *x = (const int *) a;\n\nconst int *y = (const int *) b;\n\nif(*x < *y) {\n\nreturn -1;\n\n} else if(*x == *y) {\n\nreturn 0;\n\n} else {\n\nreturn 1;\n\n}\n\n}\n\nTo order integers in the opposite direction, another comparator could reverse the comparisons or values. Even simpler, the existing comparator can be reused by flipping its arguments:\n\nint cmpIntDesc(const void *a, const void *b) {\n\nreturn cmpInt(b, a);\n\n}\n\nThese examples illustrate the standard implementation pattern of comparator functions: make the general const void * pointers into const pointers to the specific type by making an explicit type cast; use the state of the type, including one or more components when comparing structures, to determine the order; and return an integer that expresses the desired order."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.