/* Illustrate the differences between char * and an array of characters (string). */ #include #include int main() { char * s1 = NULL; char s2[20] = "abcdef"; char s3[20] = "abcdef"; int i; /* Why does this not work? */ if ( s2 == s3 ) { printf("s2 = %s equals s3 = %s\n", s2, s3); } else { printf("s2 = %s does not equal s3 = %s\n", s2, s3); } /* if */ /* Pointer arithmetic versus indexing. */ s1 = s2; for ( i = 0; i < strlen(s2); i += 1 ) { printf( "s1 = %c, s2[%d] = %c\n", *s1, i, s2[i] ); s1 += sizeof(char); } /* for */ s1 = s2; printf("Now s1 = %s\n", s1); /* Generate a segmentation fault by over running the amount of space allocated for s1. */ for (;;) { s1 = strcat(s1,s2); printf("%s", s1); } /* for */ } /* main */