Solved

C++ arrays

Posted on 2013-06-15
7
116 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
ID: 39250883
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 29

Expert Comment

by:pepr
ID: 39252815
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
ID: 39253340
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 39253389
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 29

Expert Comment

by:pepr
ID: 39255353
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 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