1. Technology
Send to a Friend via Email

Using Array Variables

What are they for?


This is the fourth tutorial in the series Online C++ Tutorial for beginners and is about enum variables. If you want to start at the beginning, begin with the first Online C++ Tutorial.

This is a rewriting of the original C++ tutorials into single page bite size tutorials which can usually be run on either codepad.org or ideone.com.

What are Arrays?

In previous tutorials, all the variables covered had been scalar. That means they hold one value. But there are many occasions when being able to hold a list of values or even a two dimensional list (grid) or three dimensions can be very useful.

This list of values is called an array and it holds the specified number of values all the same type. If a single int occupies 4 bytes then an array of 10 ints is 40 bytes in size.

Some Rules With Arrays

Arrays in C++ are pretty identical to C arrays. Remember C++ is a superset of C.

Arrays are declared like this: type variable [number of elements] syntax. For example 10 ints in a variable called primes are declared like this:

int primes[10];

You can initialize in the declaration by putting the values inside {} and separating them with commas.

int primes[10]={2,3,5,7,11,13,17,19,23,29};

You don't have to initialize every value. If you only wanted to initialize the first 8, then this would do. The last two elements are zero.

int primes[10]={2,3,5,7,11,13,17,19};

Finally you cn make it read-only by putting a const on the front.

const int primes[10]={2,3,5,7,11,13,17,19,23,29};

The compiler will generate a syntax error if you try to modify any element of primes. Here's an example program.

#include <stdio.h>

const int primes[10]={2,3,5,7,11,13,17,19,23,29};

int main(int argc, char* argv[])
    int i;
    int numelements= sizeof(primes)/sizeof(int) ;
    for ( i=0; i<numelements; i++) {
        printf("Primes[%d] = %d\n\r",i,primes[i]) ;
    return 0;

When run this outputs:

Primes[0] = 2
Primes[1] = 3
Primes[2] = 5
Primes[3] = 7
Primes[4] = 11
Primes[5] = 13
Primes[6] = 17
Primes[7] = 19
Primes[8] = 23
Primes[9] = 29

I've introduced the for statement that lets you create loops and sizeof() function that returns the size of any variable or type.

This line:

int numelements= sizeof(primes)/sizeof(int);

Declares the int variable numelements and sets it to the sizeof(primes) which is 40 divided by sizeof(int) which is 4, so numelements is set to 40/4 = 10. I could do this explicitly that's not always the best thing to do. Here's what that looks like:

int numelements = 10;

But doing it the sizeof() way is usually better. If I change the number of elements in the array say to 20, I don't have to remember to change it because the sizeof() calculations determines it correctly. Had it explicitly been declared numelements = 10 then I'd have to remember to change that as well.

Arrays Start at 0

Most computer languages, but not all e.g. Visual Basic start arrays with the first element at index 0. I always remember this as the first element is zero distance from the start of the array.

So in a ten element array the indices run from 0 to 9, not 1 to 10.

For Loops

This is a very convenient way to index through the elements of an array. It uses an index variable and has three parts all within brackets and separated by semi-colons ;

    for ( i=0; i<numelements; i++) {

In the example, snipped above the three parts are:

  1. i=0
  2. i<numelements
  3. i++

The first part is where the loop index is initialized. i=0. You can leave this blank if the loop variable has already been initialized. So this works as well:

int i=0;
for (;i<numelements; i++) {

The second part is where the loop is checked to see if it has finished. Because the last element is at index 9 (not 10), this needs to check if i < numelements. If this is true the loop continues.

Finally the third part is where the loop variable is incremented. It's traditional to increment but you can call any statement or function here, so long as the loop variable is altered. If you don't it will loop forever or until it crashes.

Infinite Loop

for (;;) {

Any or all three parts can be empty as the infinite loop above shows. There is a break statement that allows code to break out of loops and we'll cover that in a future tutorial on looping.

The Array Size is Static

The size must be specified at compile time, so you can't do this:

int a= 50;
int values[a];

Because arrays have to be declared statically. When we get to pointers in a future tutorial, you'll see how dynamic data structures can be created. You can use a #define but it's still statically declared. Like this:

#define TEN 10
int values[TEN];

That's it for arrays though we'll revisit them when we look at pointers. In the next tutorial I'm going to stay with arrays but introduce the C++ standard dynamic array known as a vector which is much more useful.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C++ Programming
  5. Learn C++ Programming
  6. Online C++ Programming Tutorial for Beginners - Lesson Four - Arrsys

©2014 About.com. All rights reserved.