Simple Structures in C

A structure is a collection of one or more variables, where each variable can be a different data type. Structures enable us to group data and work with it as a whole. Our programs are much easier to manage if we can manage several variables via a single structure.

Each structure we define can have an associated name called a structure tag. Structure tags have the same naming rules as variables, but it isn’t the same thing as a variable. A structure tag is actually a new, custom-defined data type.

#include <stdio.h>

struct intStruct{
    int a;
};

int main(void){


    int x;
    struct intStruct y;

    printf("Sizeof int = %ld\n", sizeof(x));
    printf("Sizeof int = %ld\n", sizeof(y));        

    return 0;

}

To define a structure, we use the struct statement. Note that the structure tag is actually optional.

We can then define local structure variables using the appropriate structure tag. The structure tag keeps us from having to redefine the structure members each time we want to use a particular structure.

#include <stdio.h>


struct {
    int a;
    int b;
    double c;
} x, y;

int  main(void){

    printf("Sizeof int = %ld\n", sizeof(int));
    printf("Sizeof char = %ld\n", sizeof(char));
    printf("Sizeof double = %ld\n", sizeof(double));

    printf("\nSizeof x = %ld\n", sizeof(x));
    printf("Sizeof y = %ld\n", sizeof(y));        

    return 0;

}

We can initialize structure members when we define a structure variable, and we can initialize a structure within the body of a program.

To assign a value to a structure member in a program, we use the dot operator. The dot operator is a period placed between the structure variable’s name and its member name. A structure variable name must always precede the dot operator, and a member name must always appear after the dot operator.

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


struct employee{
    int id;
    char firstName[50];
    char lastName[50];
};


int main(void){

    struct employee a;
    
    a.id = 1138;
    strcpy(a.firstName, "Joel");
    strcpy(a.lastName, "Hodgson");

    printf("%d %s, %s\n", a.id, a.lastName, a.firstName);

    return 0;

}

It is possible to define an array of a particular structure type.

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

struct student{
    char firstName[50];
    char lastName[50];
    int age;
    double average;
};

int main(void){

    struct student students[3];

    strcpy(students[0].firstName, "Ponyboy");
    strcpy(students[0].lastName, "Curtis");
    students[0].age = 14;
    students[0].average = 3.4;

    strcpy(students[1].firstName, "Sodapop");
    strcpy(students[1].lastName, "Curtis");
    students[1].age = 16;
    students[1].average = 2.7;     
    
    strcpy(students[2].firstName, "Johnny");
    strcpy(students[2].lastName, "Cade");
    students[2].age = 14;
    students[2].average = 2.5;

    printf("Name: %s, %s\n", students[1].lastName, students[1].firstName);
    printf("Age: %d\n" , students[1].age);
    printf("Avg: %f\n", students[1].average);
    
    return 0;

}

Structure variables are passed to functions by copy, not by reference. If we wish to have changes to a structure persist beyond the lifetime of the called function, we must either return the entire structure to the calling function, or else pass the structure variable by reference.

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

struct point InstantiatePoint(char *name, int x, int y);

struct point{
    char name[20];
    int x;
    int y;
};

int main(void){

    struct point points[3];

    points[0] = InstantiatePoint("ptA", 50, 100);
    points[1] = InstantiatePoint("ptB", 17, 257);
    points[2] = points[1];

    printf("%s (%d, %d)\n", points[2].name, points[2].x, points[2].y);

    return 0;

}


struct point InstantiatePoint(char *name, int x, int y){
    struct point returnPoint;
    
    strcpy(returnPoint.name, name);
    returnPoint.x = x;
    returnPoint.y = y;

    return returnPoint;
}


Note that you can copy one structure variable to another structure variable as long as both structures have the same format.

Interested in learning more about C? Take a look at my book at http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s