What Is a “Struct”?

Structures are a group of related variables that are placed under one name. Unlike arrays, structures are not limited to one data type. The struct keyword will allow us to create a structure.

Below us is a brief example of a struct, with us printing out the information we assigned the struct to.

a

Struct Code


The keyword typedef allows us to create an “alias” of a previously defined data type. The capitalized “Animal” at the end of our struct allows us to use “Animal” as a variable declaration.


The “.”, known as the dot operator, is a structure member operator. When we create a struct, we the dot operator to access the information inside the struct.

 
We can make arrays out of structs too! The below code has an entire array of the Animal data type.

b
Animal Array Code

Just like other data types, structs can be created as pointers. This is used for linked lists, which will be discussed in a later blog post. When we use struct pointers, instead of the dot operator, we use “->”, which is known as the arrow operator.

 

Below is code to print out another zoo, but this time we are using pointers.
c
Zoo Pointer Code



There are other additions we need to use pointers. The stdlib contains important functions like malloc, malloc stands for  “memory allocate”. Before memory allocation, the “zooptr” is pointing to a space in memory that hasn’t been allocated yet, which will cause a segmentation fault. After malloc has been called, space the size of the Animal struct has been allocated.

 

This is just an intro on how structures work. In the next blog post, we will go over how to create a linked list of structures, which is the basis for creating binary search trees, queues, stacks, and other “data structures”.

Author

About Josh

I love Coding!

View all posts by Josh →

3 Comments on “What Is a “Struct”?”

  1. Hi Josh,

    I happen to have two questions today. The first question is: would each new declaration (or Animal in this case) need to have every member of the struct initialized in order for the code to compile? Or would it give values of zero to the members that I didn’t specify?

    My second question is: what is happening with the “(Animal *)” on your malloc line? Wouldn’t the size of Animal stay the same (just name and location)?

    Thanks,
    James Colvin

  2. Hi James, good questions!
    First question: When we create a struct, it’s a block of memory that is the size of struct (Animal), so the variables exist even if they haven’t been given values by us. You can check this out removing
    dog.name = “Shiba Inu”;
    in the struct code file!

    Second Question: Here we have an array of pointers to the Animal struct. This is pointing to memory, but before we use malloc there is no Animal struct at that location. If you comment out the line where malloc is called, a segmentation fault is declared (Which is a memory access violation).

    I hope this helps!
    -Josh

  3. Oh okay, so that malloc line is giving two blocks of memory (the name and location of Animal) to each of the 5 places that our *zooptr is pointing to so that way the pointer isn’t pointing to something that doesn’t exist. Thanks!

    I got your code working for a chipKIT board with MPIDE as well:

    #define N 5

    typedef struct animal
    {
    char *name;
    char *location;
    } Animal;

    void setup(){
    Serial.begin(9600);
    }

    void loop(){
    int i = 0;
    char *nameArr[N] = {“Seal”, “Lion”, “Panda”, “Horse”, “Dragon”};
    char *locationArr[N] = {“North America”, “Africa”, “Asia”, “Europe”, “Camelot”};
    Animal *zooptr[N];

    for(i=0; iname = nameArr[i];
    zooptr[i]->location = locationArr[i];
    }
    Serial.println(“************ Welcome to the Zoo! ************”);
    for(i=0; iname);
    Serial.print(“It typically lives in “);
    Serial.println(zooptr[i]->location);
    }
    Serial.println(“************ Thanks for Coming! ************”);
    Serial.println(‘\n’); // extra new lines
    delay(1000); // to avoid racing through the serial monitor
    }

Leave a Reply

Your email address will not be published. Required fields are marked *