C/Structs

From Wikiversity
< C
Jump to: navigation, search

Structures allow objects of different types to be stored within one object. Let's say that you want to store a car's colour, maximum speed and registration number. We can define a structure that includes all that information.

Introduction to Structures[edit]

First, include the header files required for printf and strcpy:

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

Define the structure:

struct car
{
       unsigned int colour;
       unsigned int max_speed;
       char reg_num[10];
};

Proved example of the structure being used:

int main(void)
{

     /* define two car objects */
     struct car car_1, car_2;

     car_1.colour     = 1;   /* initialize car_1's colour */
     car_1.max_speed  = 200; /* initialize car_1's speed */

     /* initialize the reg number */
     strcpy(car_1.reg_num, "MG 100");

     /* copy car_1 data to car_2 */
     car_2 = car_1;

     /* print the values */
     printf("Colour = %u\n", car_2.colour);
     printf("Speed  = %u\n", car_2.max_speed);
     printf("Reg    = %s\n", car_2.reg_num);

     /* indicate  success */
     return 0;
}

Dynamic Structures[edit]

Along with being static, structures can also be dynamic. This means that they can be created and deleted at run time. An example is given below.

#include <stdio.h>

// This is a basic dog structure.
struct dog {
    char *name;
    char *breed;
    char *coat;
    int age;
};

// Describes the dog.
void describe(struct dog *d) {
    printf("This is a %s dog. The %s's name is %s. %s has a nice %s coat. %s is %d years old.\n",
           d->breed, d->breed, d->name, d->name, d->coat, d->name, d->age);
}

int main (void) {
    // Allocate memory for all of the dogs.
    // Notice that size for malloc is struct dog and it's casted as struct dog*.
    struct dog *dog1 = (struct dog*) malloc(sizeof(struct dog));
    struct dog *dog2 = (struct dog*) malloc(sizeof(struct dog));
    struct dog *dog3 = (struct dog*) malloc(sizeof(struct dog));

    // Give the dog's some personality.
    dog1->name = "Foxtrot";
    dog1->breed = "American Eskimo";
    dog1->coat = "white";
    dog1->age = 6;

    dog2->name = "Delta";
    dog2->breed = "German Shepherd";
    dog2->coat = "tan with a black saddle";
    dog2->age = 3;

    dog3->name = "Whiskey";
    dog3->breed = "Tamaskan";
    dog3->coat = "wolf grey";
    dog3->age = 2;

    // Tell the user about each dog.
    describe(dog1);
    describe(dog2);
    describe(dog3);

    // Free the allocated memory.
    free(dog1);
    free(dog2);
    free(dog3);

    // Return successfully.
    return 0;
}

The output should be:

This is a American Eskimo dog. The American Eskimo's name is Foxtrot. Foxtrot has a nice white coat. Foxtrot is 6 years old.
This is a German Shepherd dog. The German Shepherd's name is Delta. Delta has a nice tan with a black saddle coat. Delta is 3 years old.
This is a Tamaskan dog. The Tamaskan's name is Whiskey. Whiskey has a nice wolf grey coat. Whiskey is 2 years old.

It is now possible to do dynamic things with each dog structure, although those operations could be a little advanced. Also notice the use of the arrow operator (->). This is a cleaner way of using pointers when working with structures. For example dog1->name is equal to *(dog1).name. The arrow operator makes it easier when accessing nested structures.


Project: C
Previous: Pointers and Arrays — C/Structs — Next: Input and Output