<< Chapter < Page Chapter >> Page >
An introduction to the array index operator as used within the C++ programming language.

Array index operator in c++


int ages[5] = {49,48,26,19,16};

int my_age;

my_age = ages[2]

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 either provides the value held by the member of the array (Rvalue) or changes the value of member (Lvalue). In the above example the member that is two offsets from the front of the array (the value 26) is assigned to variable named my_age. The dereference operator of [2] means to go the 2 nd offset from the front of the ages array and get the value stored there. In this case the value would be 26. 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.

ages[3] = 20;

This is an example of changing an array’s value by assigning 20 to the 4 th member of the array and replacing the value 19 with 20. This is an Lvalue context because the array is on the left side of the assignment operator.

The C++ operator name is called the array index or simply the index operator and it uses the square brackets as the operator symbols.


array member
An element or value in an array.
An operator that allows us to reference a member of an array.
The method of referencing array members by starting at zero.

Questions & Answers

How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?