Sunday, June 3, 2012

C Structures

    Interactive applications tend to use a lot of variables.  Functions work as a way to organize commands, but data needs something similar.  Structures in C give you the ability to put several variables of different names in one variable.  This tutorial will cover how to create and use structures.

Creating:
    The written style for creating a structure is "struct name_of_struct {variable declarations;};" where name_of_struct can be replaced with any name you want.  In order to use a structure, we can declare and initialize a variable of the type "struct name_of_struct".  Once this variable is created, it has all the variables from the structure declaration.  Here is an example which creates a structure called circle:
#include <stdio.h>

//wait_key prototype
int wait_key();

//definition of circle variable type
struct circle {
    int x, y;
    double radius;
};

int main() {
    //declaring variables of type circle
    struct circle circle_A;
    struct circle circle_B;

    //set the variables within circle_A
    circle_A.x = 3;
    circle_A.y = 4;
    circle_A.radius = 5;

    //print variables within circle_A
    printf("circle A:\n");
    printf(" x position = %d\n", circle_A.x);
    printf(" y position = %d\n", circle_A.y);
    printf(" radius = %lg\n", circle_A.radius);

    printf("\n");

    //set variables within circle_B
    circle_B.x = 0;
    circle_B.y = 6;
    circle_B.radius = 3;

    //print circle_B variables
    printf("circle B:\n");
    printf(" (x, y) = (%d, ", circle_B.x);
    printf("%d)\n", circle_B.y);
    printf(" radius = %lg\n", circle_B.radius);
  
    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
circle A:
 x position = 3
 y position = 4
 radius = 5

circle B:
 (x, y) = (0, 6)
 radius = 3


    The struct circle declares the integers x and y as well as the double radius.  The variables circle_A and circle_B are of type struct circle, which means they both have x and y integers as well as a radius double.  The amount of variables and their names can be changed, but for this example circle_A and circle_B are used.

    Now that the circle variables are created, we must initialize all of their inside variables (x, y, and radius).  You can access the inside variables of a circle variable with circle_name.variable_name (circle_A.x for example).  This allows you to assign values to them and access their current values.  This example gives them values and prints them.

    In order to create a variable of type circle, we use struct circle variable_name; but the struct in front may seem redundant or unnecessary.  Many C programmers will use a work-around to avoid using struct which requires the typedef command.

Typedef'ing:
    The typedef command in C lets you invent new names for existing data structures.  For example, if you wanted to use the term uint for unsigned int, the command typedef unsigned int uint; would make this possible.  For structures, you can use typedef to remove the struct term in front of struct circle variables:
#include <stdio.h>

//create alternative names for data types
typedef struct circle circle;
typedef double real;

//wait_key prototype
int wait_key();

//definition of circle variable type
struct circle {
    int x, y;
    real radius; //is really a double
};

int main() {
    //declare circle_A of type circle
    circle circle_A;

    //assign values to inner variables
    circle_A.x = 0;
    circle_A.y = 2;
    circle_A.radius = 1;

    //print inner variables
    printf("circle_A:\n");
    printf(" centered at (%d, ", circle_A.x);
    printf("%d)\n", circle_A.y);
    printf(" radius of %lg\n", circle_A.radius);

    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
circle_A:
 centered at (0,2)
 radius of 1


    Now the variable circle_A only needs the type circle in front of it.  This was accomplished with the typedef struct circle circle; command which made the term "circle" the equivalent of "struct circle".  The "struct circle" term can still be used, but this example should demonstrate the ease of using just "circle".

    Double has also been given a synonym of "real" for this example since doubles are real numbers.  The radius of type double can now be given the type real due to the command typedef double real;.

Structures in functions:
    Custom data types like circle can be used in functions.  Structures can be both the input and output of a function, which means a function can technically return more than one number if a structure is used.  The following example has functions which use a rectangle struct:
#include <stdio.h>

typedef struct rectangle rectangle;

//wait_key prototype
int wait_key();

//definition of square variable type
struct rectangle {
    int width, height;
};

//a function which initializes inner variables
rectangle make_rectangle(int width, int height) {
    rectangle output;
  
    output.width = width;
    output.height = height;

    return output;
}

//returns the area of a rectangle
int get_area(rectangle input) {
    int area = input.width * input.height;

    return area;
}

int main() {
    //create two rectangles
    rectangle A = make_rectangle(2, 1);
    rectangle B = make_rectangle(1, 1);

    //get their areas
    int area_A = get_area(A);
    int area_B = get_area(B);

    //print the areas
    printf("Area of A = %d\n", area_A);
    printf("Area of B = %d\n", area_B);

    return wait_key();
}

//wait_key definition
int wait_key() {
    getchar();
    return 0;
}
output in prompt:
area of A = 2
area of B = 1

    This rectangle struct is simpler than the circle struct; it can be improved by adding x and y coordinates and possibly a rotation angle.  The function make_rectangle requires a width and height for input and returns a rectangle with inner variables set to the input.  The function get_area uses a rectangle input and calculates its area based on the inner variables.

    You are free to create all sorts of structures which may not necessarily be shapes.  For more information on structures, the page http://roseindia.net/c-tutorials/c-structure.shtml provides a student structure and http://www.cprogramming.com/tutorial/lesson7.html has a database example.

    The next post will cover pointers, arrays, and strings.

No comments:

Post a Comment