C qsort and pointers

qsort is a C standard library function that can sort arrays of any type in ascending order. It works by using a comparison function, specified by the user, to determine the relative order of elements in the array. This comparison function must take two const void* arguments and return an integer less than, equal to, or greater than zero, depending on whether the first argument is considered less than, equal to, or greater than the second argument, respectively.
Read more →

C const char * vs char *

The difference between const char *str and char *str is the level of protection provided for the string stored at the memory location str points to. const char *str is a pointer to a constant character. This means that the string stored at the memory location str points to cannot be modified through str. Attempting to modify the string will result in a compile-time error. char *str is a pointer to a character that is not constant.
Read more →

C strings

In C, an array of strings is typically stored in memory as a sequence of pointers to the individual strings. The individual strings themselves are stored in contiguous blocks of memory with the final character being a null terminator ('\0'), and each pointer in the array points to the start of one of these blocks. For example: char *strings[] = {"hello", "world", "!"}; Let’s assume that the array of pointers strings is stored at 0x100 and that each string is stored in memory at the following locations:
Read more →

OCaml recursive types

In OCaml, recursive types are types that are defined in terms of themselves. One common example is the definition of natural numbers using the nat type. type nat = Z | S of nat In this example, the nat type is defined as a variant type with two constructors: Z for zero, and S for the successor of a natural number. The S constructor takes a nat as an argument, hence the type nat is defined in terms of itself.
Read more →

OCaml enumerated types

In OCaml, enumerated types, also known as variant types, provide a way to define a set of distinct values that a variable can take on. Each value in an enumerated type is called a variant or a constructor. They are defined using the type keyword followed by the name of the type, an equal sign, and a set of variant constructors separated by the vertical bar |. Here’s an example of an enumerated type called color with three variants:
Read more →