Solved

C++ arrays

Posted on 2013-06-15
7
107 Views
Last Modified: 2015-05-17
I'm currently in a class, and was wondering if someone could give me a basic tutorial to arrays.  I have a general idea of what they are used for, but need help with the C++ coding.  Thanks.
0
Comment
Question by:ad_aragon
7 Comments
 
LVL 11

Accepted Solution

by:
epichero22 earned 500 total points
Comment Utility
Ehh well it's a really open question, but if you're beginning, basically an array is a container that can contain multiple values.  For example, if you had a single variable for one employee number, you can write:

int employee_number;

But if you have multiple employees, say, 5 of them, you can write:

int employee_number[5];

And each instance can contain a number for every employee.  So, you now have five variables, each of which can be accessed as follows:

employee_number[0];
employee_number[1];
employee_number[2];
employee_number[3];
employee_number[4];

Notice how the array starts at "0"....the number is referred to as an "index", and indexes always start at "0".

Anyways, there's a lot more information to arrays, but first grasp this concept and you should be OK with learning future concepts.  If you need another example, you can watch this Youtube video:

https://www.youtube.com/watch?v=gYB16A8NyB0

Is that what you're looking for?
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
You should learn that the plain old arrays are related to the C language. In C++, you may be interested in std::vector instead.
0
 
LVL 15

Expert Comment

by:Minh Võ Công
Comment Utility
0
 
LVL 16

Expert Comment

by:HooKooDooKu
Comment Utility
At its most basic, an array is a set of variables of all the same data type that are stored in memory sequentially.  The individual variables are accessed via the '[ ]' operator.

Basic Syntax
int X[10];         //Create 10 int variables
int X[0] = 1;
int X[1] = 2;
int X[2] = X[0] + X[1]
int X[9] = 10;
int X[10] = "Likely a memory access violation because you just tried to access past the end of the array.

Anther thing about arrays is that an array is simply a pointer to the 1st element of the array.  In other words, based on the above example, the following is a LOGICALLY a true statement (but perhaps not syntactically)
X = &(X[0])
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
an array is simply a pointer to the 1st element of the array

When programming, thoughs must be as accurate/clear as possible. An array, when passed as an argument, is passed as the adress of the first element. However, it differs from a pointer. The array probably occupies more space and the compiler "thinks" about the array as about a different type.

What should be emphasized in the contex, you should always pay attention to the size of the array when working with it because there is no "index out of range" automatic error checking. Also because of that std::vector and working with iterators may be both easier (especially when using the new kind of loop that comes with C++11) and less error prone.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now