Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ arrays

Posted on 2013-06-15
7
Medium Priority
?
138 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

618 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