This repository has been archived on 2026-05-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
prog-103-p-04-2030/HolidayTrip/Proficiencies/animals/insect.c
T
2026-02-13 18:37:24 +01:00

61 lines
1.4 KiB
C

#include "animals.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct insect *insect_init(const char *species, size_t size, int can_fly, int can_swim){
if(species == NULL) return NULL;
struct insect *insect = malloc(sizeof(struct insect));
if (insect == NULL) return NULL;
int index = 0;
while (*(species + index) != '\0') index ++;
insect->species = malloc((index + 1)*sizeof(char*));
if (insect->species == NULL) {
free(insect);
return NULL;
}
strcpy(insect->species, species);
insect->size = size;
insect->can_fly = can_fly;
insect->can_swim = can_swim;
return insect;
}
struct animal *animal_from_insect(const char *color, struct insect *insect){
if (color == NULL || insect == NULL) return NULL;
struct animal *animal = malloc(sizeof(struct animal));
if(animal == NULL) return NULL;
int index = 0;
while(*(color + index) != '\0') index ++;
animal->color = malloc((index + 1) * sizeof(char*));
if(animal->color == NULL){
free(animal);
return NULL;
}
strcpy(animal->color, color);
animal->type = INSECT;
animal->animal.insect = insect;
return animal;
}
void free_insect(struct insect *insect){
if (insect == NULL) return;
//Normalement il ne devrait pas etre NULL
//if(insect->species != NULL) free(insect->species);
free(insect->species);
free(insect);
}