/* * Simple example of working with strings, using both array and pointer * notation. */ #include /* print string with spaces between characters, array version */ void print_string_a(char s[]) { printf("printing '%s' using array\n", s); printf("parameter size %lu, value %p\n", sizeof s, (void *) s); int idx = 0; while (s[idx] != '\0') { printf("%c ", s[idx++]); } puts(""); } /* print string with spaces between characters, pointer version */ void print_string_p(char *s) { printf("printing '%s' using pointer\n", s); printf("parameter size %lu, value %p\n", sizeof s, (void *) s); char * p = s; while (*p != '\0') { printf("%c ", *(p++)); } puts(""); } int main(void) { char * s1 = "hello world"; char s2[] = "goodbye cruel world"; printf("string '%s' declared using pointer syntax, size is %lu\n", s1, sizeof s1); print_string_a(s1); print_string_p(s1); printf("string '%s' declared using array syntax, size is %lu\n", s2, sizeof s2); print_string_a(s2); print_string_p(s2); return 0; }