C Language – Code Analysis Practice (Intermediate Examples)
1. Intermediate C Code Analysis Examples 1.1. Pointer Size and String Length Understand how a character pointer that points to a string literal behaves. Confirm that sizeof and strlen calculate values using different criteria . #include <stdio.h> #include <string.h> int main ( void ) { char * text = " 0123456789 " ; size_t result = sizeof (text) + strlen (text); printf ( "%zu\n" , result); return 0 ; } char *text is not an array that stores the string contents; it is a pointer that stores the starting address of a string literal. sizeof(text) is the size of the pointer variable itself, which is different from the length of the string it points to. strlen(text) counts the number of characters from the location the pointer references until it reaches '\0' . The size of a pointer can vary depending on the execution environment, and this explanation uses a 64-bit environment as its basis. 1.2. Poi...