Posts

Showing posts from October, 2024

Deep C.

In this thread I will post interesting features and usages (of mostly the C programming language), that might not be known to a novice programmer. I might figure out how to display code nicely, at some other time.  typedef (declare) a struct name before defining said struct: typedef struct myStruct myStruct; struct myStruct {      myStruct* next      ... };  You can also typedef (declare) a pointer to a struct without defining the struct to achieve type safety (the pointer cannot point to a struct with a different type), and encapsulation (because it doesn't have access to the definition of said struct). Constant on the left!   if(NULL == x){...} is better than if(x == NULL){...} because then you don't get if(x = NULL){...} by mistake Declare type once: struct T *p; p = malloc(sizeof *p); instead of: struct T *p; p = malloc(sizeof (struct T)); Similarly sizeof array = (sizeof (ary) / sizeof (ary[0])) Sin...