Solved

C++ arrays

Posted on 2013-06-15
7
112 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 28

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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

896 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

16 Experts available now in Live!

Get 1:1 Help Now