Jump to content

Arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
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.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
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]
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Crane

Crane

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 398 posts
Perfect wingedpanther! Rep given.

You can also have multi-dimensional arrays
MyArray[0][1] = 1

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Dang! I didn't even know we could get rep. I wouldn't have noticed if you didn't mention it, Crane.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
kromagnon

kromagnon

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts

Quote

I know it has something to do with pointers
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 declare
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
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
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.