// sea_creature.c // // Written by YOUR-NAME (YOUR-ZID) // on TODAYS-DATE // // Introductory activity to learn malloc and free. // Creates and releases sea creatures. #include #include #include #define MAX_NAME_LENGTH 64 struct creature { char name[MAX_NAME_LENGTH]; int size; }; struct creature *create_creature(char name[], int size); void release_creature(struct creature *creature); void print_creature(struct creature *creature); void remove_newline(char input[MAX_NAME_LENGTH]); // ----------------------------------------------------------------------------- //////////////// DO NOT CHANGE THE MAIN FUNCTION /////////////////////////////// int main(void) { char name[MAX_NAME_LENGTH]; int size; printf("Enter creature name: "); fgets(name, MAX_NAME_LENGTH, stdin); // Remove trailing newline remove_newline(name); printf("Enter creature size: "); scanf("%d", &size); printf("\nCreating creature...\n"); struct creature *creature = create_creature(name, size); printf("\nCreature details:\n"); print_creature(creature); printf("\nReleasing creature...\n"); release_creature(creature); return 0; } // ----------------------------------------------------------------------------- // YOUR FUNCTIONS // ----------------------------------------------------------------------------- // Creates and returns a pointer to a new creature by calling malloc struct creature *create_creature(char name[], int size) { // TODO: Implement this function return NULL; } // Frees the creature by calling free void release_creature(struct creature *creature) { // TODO: Implement this function } // ----------------------------------------------------------------------------- // PROVIDED FUNCTIONS // ----------------------------------------------------------------------------- //////////////// DO NOT CHANGE ANY OF THE CODE BELOW HERE ////////////////////// void print_creature(struct creature *creature) { printf("Name: %s\n", creature->name); printf("Size: %d\n", creature->size); } void remove_newline(char input[MAX_NAME_LENGTH]) { int index = 0; while (input[index] != '\n' && input[index] != '\0') { index++; } input[index] = '\0'; }