/** * strings.c * * Playing with C strings. */ #include #include /** * Here we pass in a string to a function. In this case, 'str' decays to a * pointer and now we can't determine how big the array is. */ void my_func(char *str) { printf("String length is: %lu\n", strlen(str)); /* Inside the function, str is now just a pointer (decayed) */ printf("String sizeof is: %lu\n", sizeof(str)); } int main(void) { char str1[] = "Hello world"; char str2[] = "Goodbye world"; printf("%s\n", str1); printf("%s\n", str2); /* Insert a NUL byte into the middle of the string */ str1[5] = '\0'; printf("%s\n", str1); /* Print out a substring, starting from 7th character */ printf("%s\n", &str1[6]); printf("String length is: %lu\n", strlen(str2)); /* Down here, C knows that this is an array: */ printf("String sizeof is: %lu\n", sizeof(str2)); my_func(str2); /* Just for fun... how big are these data types? */ printf("sizeof(char) : %lu\n", sizeof(char)); printf("sizeof(int) : %lu\n", sizeof(int)); printf("sizeof(long) : %lu\n", sizeof(long)); printf("sizeof(int *) : %lu\n", sizeof(int*)); return 0; }