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.

Anonymous's picture

Good post and this post

Good post and this post helped me alot in my college assignement. Gratefulness you for your information.

Anonymous's picture

Pretty good post. I just

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I' ll be subscribing to your feed and I hope you post again soon.

Anonymous's picture

Good blog! I truly love how

Good blog! I truly love how it' s easy on my eyes and the facts are well written. I am wondering how I can be notified whenever a new post has been made. I have subscribed to your rss feed which should do the trick! Have a nice day!

Anonymous's picture

i have been following this

i have been following this blog for some time now, good job by the way

Anonymous's picture

It is remarkable, this

It is remarkable, this valuable message

Anonymous's picture

Usually I do not post on

Usually I do not post on blogs, but I would like to say that this article really forced me to do so! Thanks, really nice article.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.