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.
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.
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:Code:int main() { int MyArray[5]=( 1, 3, 5, 7, 9 ); return MyArray[0]; }
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]
Perfect wingedpanther! Rep given.
You can also have multi-dimensional arrays
MyArray[0][1] = 1
Dang! I didn't even know we could get rep. I wouldn't have noticed if you didn't mention it, Crane.
yes. All the elements in an array are stored consecutively in memory, and when you declare the array type, the compiler knows how far apart to allot space for them in memory. The array index(the number in the brackets) tells how much to offset the memory address from the first item in memory. For example: if i declareI know it has something to do with pointers
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)Code:int MyArray[5]
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.
Last edited by kromagnon; 07-26-2006 at 02:17 PM.
<!-- comment comment comment --></
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks