Good Practices in C programming
Use void* for pushing structures or datatype is unknown
realloc and memcpy can be used together for editing or changing the size of existing data types specially for strings and designing string library.
Always try to copy string to new array or dynamic array rather then assigning the base pointer of the array in C as string manipulation will become tough using pointer.
use dynamic array, dynamic stack, string, dictionary, priority queues, tuples as data structures by creating library
always initialize a string with calloc so that '\0' char is assigned to each index in string array
for a linked list to be used again use erase() to clean all linked list nodes
always use strncpy/strncmp instead of strcpy/strcmp
Always set '\0' at the last index of string/char array
Don't expect copy-on-write in C and don't try to rewite a string in C
rewriting an existing character array often leads to crashes , due to array out of bounds always to try to allocate & copy to a new string using strncpy.
Avoid strcat, strcpy, strstr , strtok and other library functions these always lead to crashes
But you can use only strlen.
Always try to ignore ' ' , '\v' , '\t' and '\n' inside a string, read entire file as a single large tape/array
Always initialize a char array using for loop.
use sprintf to concatenate string or conversion from one type to another instead of using string library or typecasting.
Comments
Post a Comment