Skip to main content

Student Records

Suppose you needed to store information about a student then display that information. This is a task we have done many times, but this time let us look at some deeper implications of the way we are doing this.

Firstly let us consider what student information we may wish to store...

  • first name - a C string, of at most 100 characters
  • last name - a C string of at most 100 characters
  • student number - a number
  • Program of Study - a string
  • GPA - float

This is just clearly not a complete list of information, but even if this is all we would store, we are looking at 5 separate variables. Let us store the information on a student named Amy Lee, student number 123456789, studying the CPA program and has a GPA of 3.7

What would our program look like?

int main(void)
{
char firstName[101] = "Amy";
char lastName[101] = "Lee";
int studentNumber = 123456789;
char program[5] = "CPA";
float gpa = 3.7;

printf("%s %s\n", firstName, lastName);
printf("Student #: %d\n", studentNumber);
printf("Program: %s\n", program);
printf("GPA: %f\n", gpa);
}

Consider the way we had to declare the variables.. each of our pieces of information required a separate variable to be stored.

Now consider what if we wanted to add a second student to the mix. A student named "Bob Jones", student number 987654321 in the CPP program with a GPA of 3.73.

What would our program look like then?

int main(void)
{
char firstName[101] = "Amy";
char lastName[101] = "Lee";
int studentNumber = 123456789;
char program[5] = "CPA";
float gpa = 3.7;

char firstName2[101] = "Bob";
char lastName2[101] = "Jones";
int studentNumber2 = 987654321;
char program2[5] = "CPP";
float gpa2 = 3.73;

...

}

We would need a complete set of these variables for each student we have.

Even in this simple representation with only 5 pieces of information and just 2 students, we can see how unwieldy this can get. We need something better than this to organize this information better

In C, we can package related data into one single entity called a struct. Similar to an array, a struct is an aggregate data type. One way to think about a struct, is that it allows us to essentially create our own simple custom data type. Using structs we can declare a single variable to store all required data for that custom type. To start, we begin by making a defining the struct.

Struct Definition

To use structs, we must first define what that struct will store.

The syntax is:

struct NameOfStruct
{
datatype1 dataMember1;
datatype2 dataMember2;
...
};

To define a Student struct to store the same information we previously stored, we would do the following:

struct Student
{
char firstName[101];
char lastName[101];
int studentNumber;
char program[5];
float gpa;
};

The above code does not create any variables. It only states that if we were to create variables of type Student, then that variable has all the information stated within a struct.

We call the data within a struct data members. Thus in the above struct, firstName, lastName, studentNumber, program and gpa are all data members of Student.

note

Styling convention is to use UpperCamelCase for the name of the struct. This means that every word in the name begins with an upper case character. The opening curly bracket of a struct is also placed on the next line similar to functions. data member names should use lowerCamelCase.

Instantiation of a Struct

Defining a struct defines what data is part of any variables of that struct type. It is not a variable and no memory is created at that point. In order to use this struct we must create instances of this struct. The following is the syntax of how to do this:

	struct NameOfStruct variableName;

Thus if we wanted to create an instance of the Student struct, we would use the following syntax:

	struct Student student1;

The above line of code reserves space in memory to hold a student. The following is created after the line above:

image of struct Student student1

Initializing a Struct

When you instantiate a struct, you can initialize the data members of the struct using the same curly bracket notation like arrays. Each value will be assigned to the data members in the order that the data members are declared. We can therefore initialize a student to the information for Amy as follows:

	struct Student student1 ={ "Amy", "Lee", 123456789, "CPA", 3.7};

The above code will create the following in memory

image of struct Student student1

Accessing data members

When you create an instance of a struct, it contains all of the data members in the struct definition. To access any one data member given a struct, we use the dot(.) operator

Syntax:

	... instanceVariable.datamember ...

For example, here is our original program in struct form:

struct Student
{
char firstName[101];
char lastName[101];
int studentNumber;
char program[5];
float gpa;
};
int main(void)
{
struct Student student1 ={"Amy", "Lee", 123456789, "CPA", 3.7};
//one way to read this to yourself is to treat the . operator like an the "'s" possessive
//construct in english "student1.firstName" is "student1's firstName"
printf("%s %s\n", student1.firstName, student1.lastName);
printf("Student #: %d\n", student1.studentNumber);
printf("Program: %s\n", student1.program);
printf("GPA: %f\n", student1.gpa);
}

Structs provide a convenient way for us to group together data. The data can be of different types (unlike an array).