• Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

# ? about arrays

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1joey201  Members   -  Reputation: 106

Like
0Likes
Like

Posted 25 June 2012 - 02:21 PM

Hello I'm following this tutorial about arrays and I understand how they are declared and the several ways to initialize them, what confuses me is when I can't do
`iaArray1 = iaArray2;`
```int iaArray1[3] = {1,2,3};
int iaArray2[3];
int iIndex = 0;
while (iIndex < 0) {
iaArray2[iIndex] = iaArray1[iIndex];
}
```
could I have done this?:
```int iaArray1[3] = {1,2,3};
int iaArray2[3];
int iIndex = iaArray1;
int iaArray2 = iIndex;
}
```
What I understand is the first one is not possible the second one iaArray1 equals iaArray2 as long as iIndex is less than 0. In the third one (the one I made up) its saying iIndex equals iaArray1 and iaArray2 equals iIndex.
Anyway if anyone could develop a bit more I will greatly appreciate it.

### #2BCullis  Members   -  Reputation: 1668

Like
1Likes
Like

Posted 25 June 2012 - 02:32 PM

Just for general information background: an array is a contiguous block of memory that, through its type declaration, knows how to partition the memory it spans when a read-request or write-request is made. The variable name you give an array (like "iaArray1") refers to the head of the array (a pointer to the start of the contiguous memory block), and the index operator ( the [#] part) says "start at the head, and jump # appropriately-sized** chunks of memory before you read or write the value of that memory".

More specifically: your while loop doesn't do anything. You set iIndex to 0, and your while condition is "iIndex < 0", so it will never run. What you probably need to look at is for-loops. I'm assuming you want to copy the contents of iaArray1 into iaArray2? That would take something like this:
```int iaArray1[3] = {1,2,3};
int iaArray2[3];
for(int iIndex = 0; iIndex < 3; i++)
{
iaArray2[iIndex] = iaArray1[iIndex];
}
```

**Technically, this is " # * sizeof(type)" where in your example type is "int"

Edited by BCullis, 25 June 2012 - 02:33 PM.

Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too

### #3frob  Moderators   -  Reputation: 7540

Like
1Likes
Like

Posted 25 June 2012 - 02:34 PM

Assuming C++...

You need to keep in mind how arrays work. Arrays are a continuous chunk of memory, and the variable name is basically a pointer to the beginning of that memory.

For your first line: iaArray[3] = {1, 2, 3}, the values are stored as data in the executable. They are assumed to be constant (although only some compilers will warn about it). The variable iaArray effectively addresses that chunk of memory.

The second line iaArray2[3] allocates a separate chunk of memory. That chunk of memory automatically exists when you hit the line. When you attempt to do iaArray2=iaArray1, think about what you are doing. You have a chunk of memory, and you want to ... what exactly? Do you want to copy the memory as raw data? Do you want to point to that memory? Do you want to destroy whatever you have in your memory and make copies of the objects held in the other block of memory? None of that really makes sense under the hood. You already have an array of objects that are already created.

If you want to point to the chunk of memory, use an integer pointer.
If you want to copy raw data, use memcopy.
If you want to make duplicates of the data, try std::copy()

Edited by frob, 25 June 2012 - 02:37 PM.

### #4SabreMan  Members   -  Reputation: 502

Like
-2Likes
Like

Posted 26 June 2012 - 02:33 AM

Hello I'm following this tutorial about arrays and I understand how they are declared and the several ways to initialize them, what confuses me is when I can't do
...

Arrays in C++ are a degenerate data structure which you should avoid, at least if working code is a priority. You might consider finding a tutorial which teaches a more modern approach to C++ (Accelerated C++ for instance).

### #5SiCrane  Moderators   -  Reputation: 6621

Like
0Likes
Like

Posted 26 June 2012 - 03:24 AM

Incidentally, the formal way to say this is that arrays are not first class types in C++ (or C for that matter).

### #6Matt-D  Members   -  Reputation: 841

Like
0Likes
Like

Posted 26 June 2012 - 10:27 AM

You should consider using containers:
- for fixed-size arrays, use std::array: http://en.cppreferen...container/array
- for dynamic (resizable) arrays, use std::vector: http://en.cppreferen...ontainer/vector

For example: http://ideone.com/OzBtb

With an optimizing compiler there should be absolutely no performance difference between C-style fixed-size array, say "int Array[5]" and, say, "std::array<int, 5> Array" -- none, as in: they compile to an identical machine code.

With containers you can do some pretty cool stuff -- e.g., if you want to implement linear algebra expressions such as "int i = (A + B)[2];" that evaluates to "int i = A[2] + B[2];" in an efficient way (not touching the elements you don't need for the result), you can use Expression Templates or Boost.Proto: http://cpp-next.com/...n-optimization/ (although this is no longer strictly "for beginners", save the link for the future, some day it may come in handy).

Edited by Matt-D, 26 June 2012 - 10:43 AM.

### #7antiHUMANDesigns  Members   -  Reputation: 58

Like
0Likes
Like

Posted 26 June 2012 - 10:29 AM

You can access the elements of an array is a second way aswell.

[source lang="cpp"]int array[10]; // create the arrayint *p = array; // create a pointer of the same type and assign it to the start of the array.[/source]

Now, using pointer arithmetics, you can access the elements by adding a number to the pointer, such as "p+1", which is the same thing as array[1]. "p" by itself would be the same thing as array[0]. This actually what arrays do, but I think the compiler keeps a note on how many elements the array was created with, to help you with not going out of bounds.
Also, you need to "delete" arrays that have been created with "new" by typing "delete[]".

Pointer ariothmetics is not just an alternative way of traversing an array, but is something more or less needed, so it's good to know it.

I'm a bit rusty on programming atm, so don't bash me too hard if I wrote something stupid or made some silly mistake.

EDIT: oh, and using an array normally (array[0]) dereferences automatically, but if you use "p", it's not dereferenced, so you need to write "*p" to get the actual value of the element, instead of the address to it. Just wanted to point that out, even if I felt it was obvious.

Edited by antiHUMANDesigns, 26 June 2012 - 10:37 AM.

### #8joey201  Members   -  Reputation: 106

Like
0Likes
Like

Posted 26 June 2012 - 06:52 PM

Thanks all for the replies. I'm following these online tutorials and while they have given me an insight on the language I think I may get a book to get a more professional approach and not have this type of confusions on the journey. Which ones do you reccomend I'm inclining towards Accelerated C++ SabreMan suggested.

### #9frob  Moderators   -  Reputation: 7540

Like
0Likes
Like

Posted 27 June 2012 - 10:42 AM

Which ones do you reccomend I'm inclining towards Accelerated C++ SabreMan suggested.

That is a very good book to start with.

### #10Matt-D  Members   -  Reputation: 841

Like
0Likes
Like

Posted 27 June 2012 - 11:57 AM

Which ones do you reccomend I'm inclining towards Accelerated C++ SabreMan suggested.

That is a very good book to start with.

Agreed!
OP, if you have programmed before, "Accelerated C++" is the one I would recommend as well.