Skip to content

Instantly share code, notes, and snippets.

@mu8086
mu8086 / qsort_strings.c
Created June 19, 2022 05:42
[C] qsort strings
int compare(const void *a, const void *b) {
const char *aa = *(const char **)a;
const char *bb = *(const char **)b;
return strcmp(aa, bb);
}
qsort(strings, stringsSize, sizeof(char *), compare);
@mu8086
mu8086 / variadic_free.c
Created June 12, 2022 12:28
[C] variadic free function
void doFree(void * allocate, ...) {
void *target = allocate;
va_list args;
va_start(args, allocate);
while (target != NULL) {
free(target);
target = va_arg(args, void *);
}
@mu8086
mu8086 / 3dArray.c
Created June 5, 2022 00:05
[C] 3dArray
char ***char3dArray(int length, int width, int height) {
// ret[height][width][length]
char ***ret = (char ***) malloc(sizeof(char **) * height + sizeof(char *) * height * width + sizeof(char) * height * width * length);
char **first_row = (char **)(ret + height);
char *first = (char *)(first_row + height * width);
memset(first, 0, sizeof(char) * height * width * length);
for (int i=0, j, wl = width * length; i<height; ++i) {
ret[i] = first_row + i * width;
@mu8086
mu8086 / listReverse.c
Last active May 21, 2023 13:13
[C] List Reverse
int listReverse(struct ListNode** head) {
int node_count = 0;
struct ListNode *tmp = NULL, *walker = *head;
if (walker != NULL) {
node_count = 1;
while (walker->next != NULL) {
tmp = walker->next;
walker->next = tmp->next;
tmp->next = *head;
@mu8086
mu8086 / returnColumnSizes.c
Last active May 12, 2022 09:25
[C] LeetCode how to return (int** returnColumnSizes)
int* getReturnColumnSizes(int row, int col) {
int* returnColumnSizes = (int*) malloc(sizeof(int) * 1 * row);
for (int i=0; i<row; i++) {
returnColumnSizes[i] = col;
}
return returnColumnSizes;
}
@mu8086
mu8086 / 2dArray.c
Last active October 9, 2022 01:12
[C] Allocate 2D Array
int ** int2dArray(int row, int col) {
int i, **ret = (int **) malloc(sizeof(int *) * row + sizeof(int) * row * col);
for (i = 1, ret[0] = (int *)(ret + row); i < row; ++i) {
ret[i] = ret[i-1] + col;
}
return ret;
}
@mu8086
mu8086 / template.bash
Last active May 12, 2022 09:05
[Bash] Dynamically expanded bash menu that can call any function
#!/bin/bash
shopt -s extglob
SCRIPT_NAME="$(basename $0)"
LOG_FILE="${0%.*}.log"
DATE_FORMAT="%F %H:%M:%S %z"
PID_MAX="$(cat /proc/sys/kernel/pid_max)"