Learning C++: Arrays, Enumerations, Structs and Unions (C++)

An example "Hello World" in C++

In this blog post I am going to take a look at arrays, enumerations, structs and unions within C++. As with my previous post I am creating this blog entry as notes to myself for reference, but they also benefit others.

First though some excellent (for me) news, on my C++ learning path (started in my last blog entry) I have completed DEV210x: Introduction to C++ via EdX

geektechstuff_cpp_intro_cert
I’ve passed DEV210x: Introduction to C++

Arrays

A set of objects (all of the same type) grouped and managed together. The array index starts at 0 (zero indexed), and the size of the array depends on the number of elements it can contain.

An array can be created via different methods. For each of the below the array’s name is myArray:

int myArray[10];

Creates an array and declares it to have 10 element slots, the values will default to 0 for initialisation.

int myArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Creates an array, again with 10 element slots and intializes them with values (1 through 10). Note: As the array is zero indexed the value of 1 is stored in index position 0 and the value of 10 is stored in index position 9).

int myArray[10] = {1, 2, 3, 4};

Creates an array, again with 10 element slots and initialises the first 4 index positions with values 1 through 4 (with 1 being in index position 0 and 4 being in index position 3). The rest of the positions store the default value of 0.

The default value depends on the data type being stored in the array, e.g. default of 0 is for the int data type.

The view data in an array the data’s index position can be used, e.g. outputting index position 3 of myArray[10]={1, 2, 3, 4} will output the number 4.

outputting index position 3 of myArray[10]={1, 2, 3, 4} will output the number 4.
outputting index position 3 of myArray[10]={1, 2, 3, 4} will output the number 4.
Or a for loop can be used to print the values of array:

C++ For Loop looping through the first 4 values of the array
C++ For Loop looping through the first 4 values of the array

 

Enumerations

Created using the keyword enum and can be used to set expected values so that other values cannot be entered, e.g.

enum Months {January, February, March, April, May, June, July, August, September, October, November, December};

Would create an enum called Months containing the values of January, February etc… which would be assigned values starting at 0. So January = 0, February = 1.

The Months enum could then be used to set a variable, e.g. a variable called foolsMonth which would only accept the months as values it could contain.

Months foolsMonth;

foolsMonth = April;

Although foolsMonth has been assigned the string April it would output this as value 3 as April is in slot 3 of the enum. Note: Remember this is zero based indexing, so the first value goes into slot 0.

To change the values so that they do not start at zero, set the initial value when defining the enum, e.g.

enum Months {January=1, February, March, April, May, June, July, August, September, October, November, December};

This would set January’s value to 1, February to 2, March to 3, etc..

Structs

Where as Arrays can store multiple entries of only one data type (e.g. ints), a structure or struct can store multiple entries of multiple data types. For example to create a struct called comicBook to hold the details of a comic book;

struct comicBook

{

int issue;

int volume;

string publisher;

};

The type comicBook can then be used via the following method:

comicBook xMen = {1, 2, "Marvel"};

Which sets the values of issue 1, volume 2 and publisher Marvel to xMen or an alternative method is:

comicBook fantasticFour;

fantasticFour.issue = 4;

fantasticFour.volume = 1;

fantasticFour.publisher = "Marvel";

Which sets the values of issue 4, volume 1 and publisher Marvel to fantasticFour.

Data can be accessed from the strut using its name, for example to access the publisher of Fantastic Four (Marvel) I can ask C++ to output fantasticFour.publisher:

Asking C++ to output fantasticFour.publisher
Asking C++ to output fantasticFour.publisher

Unions

A union is very similar to a struct in that it can hold multiple different data types, however unlike a struct, a union can only hold one value at a time.

union heisenberg

{
      int position;

     double momentum;

};

This would set up a union called particle, however it can only hold one of the values at a time. So I could set the position using:

heisenberg.position = 2;

But if I then set the momentum using:

heisenberg.momentum = 3.6

Then the value of heisenberg.position will be lost, e.g. only one field can hold data at a time.

I’ve tried to use an example of the Heisenberg Uncertainty Principle here as Unions can be a little weird and my understanding is that they are generally used in programs that have limited memory.

2 thoughts on “Learning C++: Arrays, Enumerations, Structs and Unions (C++)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.