Skip to main content
Engineering LibreTexts

19.3: Displaying Array Members

  • Page ID
    10361
  • Accessing Array Members in C++

    Accessing the members of an array
    int ages[] = {49,48,26,19,16};
    int counter;
    
    for (counter = 0, counter < 5, counter++)
      {
      cout << ages[counter] << endl;
      }
    

    This second usage of the square brackets is as the array notation of dereference or more commonly called the index operator. As an operator it provides the value held by the member of the array. For example, during one of the iterations of the for loop the index (which is an integer data type) will have the value of 3. The expression ages[counter] would in essence be: ages[3]. The dereference operator of [3] means to go the 3rdoffset from the front of the ages array and get the value stored there. In this case the value would be 19. The array members (or elements) are referenced starting at zero. The more common way for people to reference a list is by starting with one. Many programming languages reference array members starting at one, however for some languages (and C++ is one of them) you will need to change your thinking. Consider:

    Position C++ Miss America Other Contestents
    zero offsets from the front ages[0] Winner 1st Place
    one offsets from the front ages[1] 1st Runner Up 2nd Place
    two offsets from the front ages[2] 2nd Runner Up 3rd Place
    three offsets from the front ages[3] 3rd Runner Up 4th Place
    four offsets from the front ages[4] 4th Runner Up 5th Place

    Saying that my cousin is the 2nd Runner Up in the Miss America contest sounds so much better than saying that she was in 3rd Place. We would be talking about the same position in the array of the five finalists.

    Rather than using the for loop to display the members of the array, we could have written five lines of code as follows:

    cout << ages[0] << endl;
    cout << ages[1] << endl;
    cout << ages[2] << endl;
    cout << ages[3] << endl;
    cout << ages[4] << endl; 
    

    Using the Sizeof Operator with Arrays in C++

    Using the sizeof operator
    int ages[] = {49,48,26,19,16};
    int counter;
    
    for (counter = 0, counter < sizeof ages / sizeof ages[0], counter++)
      {
      cout << ages[counter] << endl;
      }
    

    Within the control of the for loop for the displaying of the grades, note that we calculated the number of the members in the array by using the sizeof operator.  The expression is:

    sizeof ages / sizeof ages[0]

    When you ask for the sizeof an array identifier name the answer is how many total bytes long is the array (or in other words – how many bytes of storage does this array need to store its values).  This will depend on the data type of the array and the number of elements.  When you ask for the sizeof one of its members, it tells you how many bytes one member needs.  By dividing the total number of bytes by the size of one member, we get the answer we want: the number of members in the array.  This method allows for flexible coding.  By writing the for loop in this fashion, we can change the declaration of the array by adding or subtracting members and we don't need to change our for loop code.

    Demonstration Program in C++

    Creating a Folder or Sub-Folder for Source Code Files

    Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

    • Demo_Programs

    If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

    Download the Demo Program

    Download and store the following file(s) to your storage device in the appropriate folder(s). Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials.

    Download from Connexions: Demo_Arrays.cpp

    Definitions

    Flexible Coding
    Using the sizeof operator to calculate the numer of members in an array.