C++ Arrays

Arrays are used to store a number of items in one single variable. They are usually placed in the same segment of memory with accessible elements for each variable. Think of a bead as a variable and a bead necklace as an array. This article will explain to you the tricks to C++ arrays.

Let's say you want to fill up an array (necklace) with a number of values (beads).

#include <iostream>
using namespace std;

int main(){
  int necklace[50];
  return 0;
}

This allocates 50 elements of memory in the stack (since it is an integer: it is 50 bytes) called the array necklace.

#include <iostream>
using namespace std;

int main(){
  int necklace[3] = { 5, 1, 4}
  // the above is the same as below for necklace2:
  int necklace2[3];
  necklace2[0] = 5;
  necklace2[1] = 1;
  necklace2[2] = 4;

  cout << "Necklace and Necklace2 are equivalent!";
  return 0;
}

Using the second way to declare the array, you can skip indexes (in the following example we skipped index 3 and index 5).

#include <iostream>
using namespace std;

int main(){
  int necklace[50];
  necklace[0] = 43;
  necklace[1] = 321;
  necklace[4] = 500;
  necklace[6] = 4;
  return 0;
}

Remember that an array[3] can have 4 different values, since the index starts from zero.

More Complicated Way of using Arrays

An array is actually a variable much like a pointer. The following code will work the same way as before:

#include <iostream>
using namespace std;

int main(){
  int necklace[50];
  *(necklace) = 43; // first element
  necklace[1] = 321;
  *(necklace+4) = 500;
  necklace[6] = 4;
  cout << "The fifth variable is: " << *(necklace+4) << " it's same as: " << necklace[4] << endl;
  // this will output 500 in both places.
  return 0;
}

Try the code in code::blocks.

Multidimensional Arrays

Arrays can be multidimensional much like matrices.

#include <iostream>
using namespace std;

int main(){
  int necklace[50][40][30];
  necklace[5][0][0] = 50;
  necklace[5][0][1] = 550;
  return 0;
}

You can loop through them easily, you can place variables in each [] bracket and then change the variables.

Arrays as Function Parameters

Arrays can be as parameters inside functions. However, you must give the size of the array to the function because it cannot tell how large the array is through "sizeof".

#include <iostream>
using namespace std;

void displayArray(int array[], int sizeOfArray){
  for(int i = 0; i < sizeOfArray; i++){
    cout << "Index: " << i << " :: Value: " << array[i] << endl;
  }
}

int main(){
  int necklace[50];
  necklace[0] = 10;
  necklace[1] = 4;
  necklace[2] = 50;
  int sizearray = 3;
  // DO NOT try to access elements that are not declared.
  displayArray(necklace, sizearray);
  return 0;
}

Don't try to loop through an array in places where it isn't defined, because you'll get a crash (segmentation fault).

Determining Size of an Array

You can determine the size of an array in this way:

#include <iostream>
using namespace std;

int main(){
  int necklace[4];
  necklace[0] = 5;
  necklace[1] = 6;
  necklace[2] = 9;
  necklace[3] = 10;
  cout << "Size Of Array: " << sizeof(necklace)/sizeof(necklace[0]);
  return 0;
}

necklace[0]'s size is an integer's size, which is simply 4 bytes. If it was char, it would be 1 byte. This should give you the correct value of the size (4 in this case because 16/4 = 4).

You cannot tell the size of an array if it is passed into another function.

copy dvd's picture

It is remarkable, this

It is remarkable, this valuable message

Anonymous's picture

how can i make program using

how can i make program using arrays related to my course civil engineering?

rTabon2003's picture

"Of course, what a excellent

"Of course, what a excellent website and useful articles, I will create backlink - bookmark this website? Regards,Reader."

kLuana2000's picture

Fantastic publish. I just

Fantastic publish. I just discovered your internet site and would prefer to say that I've definitely cherished reading via your weblog posts. At any fee I am heading to be subscribing to your feed and that i seriously hope you jot down again quickly.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.