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

Array index operator in c++

Example:

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.

Definitions

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

Questions & Answers

I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
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