Solved

C++ arrays

Posted on 2013-06-15
7
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

707 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