<< Chapter < Page Chapter >> Page >
Examples of displaying members of an array with for loops and using the sizeof operator within the C++ programming language.

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; }

Got questions? Get instant answers now!

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 3 rd offset 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 Contests
zero offsets from the front ages [0] Winner 1 st Place
one offsets from the front ages [1] 1 st Runner Up 2 nd Place
two offsets from the front ages [2] 2 nd Runner Up 3 rd Place
three offsets from the front ages [3] 3 rd Runner Up 4 th Place
four offsets from the front ages [4] 4 th Runner Up 5 th Place

Saying that my cousin is the 2 nd Runner Up in the Miss America contest sounds so much better than saying that she was in 3 rd 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; }

Got questions? Get instant answers now!

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 number of members in an array.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?

Ask