#include #include #include /* * show how to create dynmic strings */ int main() { char **pets; // a pointer to an array of ptrs int i; // index for array int capacity; // total space in array int num_pets; // used spaces in arrays char str[BUFSIZ]; // input buffer char *newstr; // pts to allocated string printf("How many pets in your zoo? "); fgets(str, BUFSIZ, stdin); capacity = atoi(str); pets = malloc( capacity * sizeof( char * ) ); i = 0 ; while (1 ) { printf("next pet name? "); fgets(str, BUFSIZ, stdin); if ( str[0] == '\n' ) break; str[strlen(str)-1] = '\0'; // remove \n newstr = malloc( strlen(str) + 1); // 1 for \0 pets[i] = newstr; strcpy(newstr, str); i++; } num_pets = i; for ( i = 0 ; i < num_pets ; i++ ) printf("pet[%d] is %s\n", i, pets[i]); return 0; }