/* plus.c - Let's look at what + means in C using arrays. * The same concept also works for strings. */ #include #include /* before_zero - How many elements do we see in the array beore the first zero? * Precondition: We assume the number 0 exists in the array. */ int before_zero(int a[]) { int i, how_many = 0; for (i = 0; ; ++i) { if (a[i] == 0) break; ++how_many; } return how_many; } main() { int a[] = { 5, 4, 7, 3, 2, 0, 6, 1, 8, 9 }; int count; char s[] = "ice cream"; count = before_zero(a); printf("I found %d elements of a before its first zero.\n", count); count = before_zero(a + 3); printf("I found %d elements of a+3 before its first zero.\n", count); printf("The length of the string s = '%s' is %d.\n", s, strlen(s)); printf("The length of the string s+3 = '%s' is %d.\n", s+3, strlen(s+3)); } /* Output: I found 5 elements of a before its first zero. I found 2 elements of a+3 before its first zero. The length of the string s = 'ice cream' is 9. The length of the string s+3 = ' cream' is 6. */