C++

C++ - Weird & Interesting Stuff I found

Oh boy, C++ is weird ... but also interesting :)

Posted by Rico's Nerd Cluster on January 5, 2023

Strings

  • Raw C strings (char arrays) should always terminate with a null character, ‘\0’. Fun fact about char arrays: nullptr is inserted at the back of the array. This is a convention for argv
1
2
3
4
5
6
7
8
char str[] = "a string";
printf("%s", str);  // searches for "\0" to terminate

char *strs[] = {"str1", "str2", nullptr};
for (size_t i = 0; strs[i] != nullptr; ++i){
    // uses nullptr to terminate string array.  
    printf("%s", strs[i]);
}