// A demonstration of structs and memory allocation
// using characters from Avatar the Last Airbender 

// Marc Chee (cs1511@cse.unsw.edu.au), October 2020

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 1024
#define TEAM_SIZE 3

// STRUCTS
// Remember that structs are definitions of a new type.
// Every time we want to use a struct, we have to declare
// a variable of that type.

// a bender is a person with power over a certain element
struct bender {
    char name[MAX_LENGTH];
    char element[MAX_LENGTH];
    int power;
};

// our team struct will store an array of pointers to benders
struct team {
    char name[MAX_LENGTH];
    int numMembers;
    struct bender *teamMembers[TEAM_SIZE];
};

// FUNCTIONS
struct bender *createBender(char *inputName, char *inputElement, int inputPower);
void printTeam(struct team *printTeam);
void freeTeam(struct team *fTeam);

int main(void) {
    // allocate memory for one team
    struct team *benderTeam = malloc(sizeof (struct team));
    strcpy(benderTeam->name, "Avatar's Team");
    
    // storing the pointers that are the result of createBender
    // in elements of the team's array
    benderTeam->teamMembers[0] = createBender("Aang", "Air", 10);
    benderTeam->numMembers = 1;
    benderTeam->teamMembers[1] = createBender("Katara", "Water", 6);
    benderTeam->numMembers++;
    benderTeam->teamMembers[2] = createBender("Sokka", "None", 2);
    benderTeam->numMembers++;
        
    printTeam(benderTeam);
    freeTeam(benderTeam);
    
    // an example of a struct variable that's not memory allocated
    struct bender zuko;
    strcpy(zuko.name, "Prince Zuko");
    strcpy(zuko.element, "Fire");
    zuko.power = 9;

}

// createBender will allocate memory for a struct bender
// It will populate the fields with the input information
// It will return a pointer to the allocated memory
struct bender *createBender(char *inputName, char *inputElement, int inputPower) {
    struct bender *newBender = malloc(sizeof (struct bender));
    
    strcpy(newBender->name, inputName);
    strcpy(newBender->element, inputElement);
    newBender->power = inputPower;
    
    return newBender;
}

// printTeam will print out the details of the team members
// to the terminal. It will not change the team.
void printTeam(struct team *printTeam) {
    printf("Team name is %s\n", printTeam->name);
    int i = 0;
    while (i < printTeam->numMembers) {
        printf("Team member %s uses the element: %s\n", 
               printTeam->teamMembers[i]->name,
               printTeam->teamMembers[i]->element
        );
        i++;
    }
}

// freeTeam will free all the memory used for a team.
// It will first free all members, then the team itself
void freeTeam(struct team *fTeam) {
    int i = 0;
    while (i < fTeam->numMembers) {
        free(fTeam->teamMembers[i]);
        i++;
    }
    free(fTeam);
}



Resource created Tuesday 27 October 2020, 03:01:55 PM.

file: benders.c


Back to top

COMP1511 20T3 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G