I've just heard of this, and I know it has something to do with pointers (but I don't even know what pointers do yet). I know arrays also have positions, and I'm guessing pointers "point" to arrays in different positions, but I am really unsure.
Arrays
Started by Sionofdarkness, Jul 21 2006 10:17 AM
5 replies to this topic
#1
Posted 21 July 2006 - 10:17 AM
|
|
|
#2
Posted 21 July 2006 - 02:06 PM
An array is a variable that stores multiple pieces of data of the same type. This corresponds to the idea that an apartment building can store more than one family at the same address. Since each family needs to get their own mail, they are referenced by apartment number. Arrays use a similar technique to access their members.
MyArray[0] == 1
MyArray[1] == 3
MyArray[2] == 5
MyArray[3] == 7
MyArray[4] == 9
The name MyArray can be used as a pointer that points to MyArray[0]
int main()
{
int MyArray[5]=( 1, 3, 5, 7, 9 );
return MyArray[0];
}
This snippet sets up an array of 5 ints, all referred to by the name MyArray. The indices in C/C++ are 0,1,2,3,4. The individual elements of the array are:MyArray[0] == 1
MyArray[1] == 3
MyArray[2] == 5
MyArray[3] == 7
MyArray[4] == 9
The name MyArray can be used as a pointer that points to MyArray[0]
#3
Posted 24 July 2006 - 06:04 PM
Perfect wingedpanther! Rep given.
You can also have multi-dimensional arrays
MyArray[0][1] = 1
You can also have multi-dimensional arrays
MyArray[0][1] = 1
#4
Posted 25 July 2006 - 02:58 PM
Dang! I didn't even know we could get rep. I wouldn't have noticed if you didn't mention it, Crane.
#5
Posted 26 July 2006 - 01:12 PM
Quote
I know it has something to do with pointers
int MyArray[5]then each item in the array is 32 bits, so the first item: (MyArray[0]) is 0 bits away from the beginning of the array, while the 3rd item in the array (MyArray[2]) is 2*32 = 64 bits away from the first item in the array, so the address in memory is ((address of first element) + 64)
if an array is used incorrectly, you will read memory outside the bounds of the array, for example, if I try to find the value of MyArray[9] , it will give me a value, it will just turn whatever junk is stored in those 32 bits in memory into an int. You can also access memory in front of the array as well, for example: MyArray[-1] will return the value 32 bits before the first item.
<!-- comment comment comment --></
#6
Posted 26 July 2006 - 02:35 PM
When learning C++, pointers was the hardest concept for me to get. But once I got on track, it made my Data Structures class so much easier.


Sign In
Create Account


Back to top









