/* string2.c: an example with arrays of characters/strings in C Laura Toma */ #include #include int main() { char c; char a[3]; int j; /* initialize a */ a[0] = 'a'; a[1] = 'b'; a[2] = 'c'; /* print the characters, one by one*/ for (j=0;j<3;j++) { printf("char %2d: %c\n", j, a[j]); } /* a[] is an array of characters, but not yet a string. Attempting string operations on a[] will print junk, give a segfault or bus error */ /* error */ printf("you typed: %s\n", a); /* error */ printf("length of a is %d\n", strlen(a)); /* need set the EOS */ a[4]='\0'; /* will this work? why not? what do we need to do? */ /* now you can print it as a string.. */ printf("you typed: %s\n", a); printf("length of a is %d\n", strlen(a)); return 0; }