c语言不常用写法集锦

想说奇淫技巧来着,想着其实只是些不常见的写法而已,并不值得推崇和觉得高明,只是整理出来给大家涨个见识,看个乐而已。

不用memset和bzero清零结构体

t_mystruct mystruct = {};

或者

t_mystruct mystruct;
mystruct = (t_mystruct){};

index[array]来替代array[index]

int ft_strlen(char *str) {
    int i = 0;
    while (i[str])
        ++i;

    return i;
}

负索引数组

#include <limits.h>   // INT_MAX
#include <stdio.h>    // printf

int main(void) {
    int x[2001];
    int *y = &x[1000];

    (void)x;
    y[-10] = 5;
    printf("%d\n", y[-10]);
}