Closed Thread
Results 1 to 6 of 6

Thread: Arrays

  1. #1
    Sionofdarkness is offline Programming Expert
    Join Date
    Jul 2006
    Posts
    383
    Rep Power
    0

    Arrays

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143
    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.
    Code:
    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

  4. #3
    Crane's Avatar
    Crane is offline Programming Expert
    Join Date
    Nov 2005
    Posts
    398
    Rep Power
    25
    Perfect wingedpanther! Rep given.

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

  5. #4
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143
    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

  6. #5
    kromagnon is offline Learning Programmer
    Join Date
    Jun 2006
    Posts
    50
    Rep Power
    0
    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
    Code:
    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.
    Last edited by kromagnon; 07-26-2006 at 02:17 PM.
    <!-- comment comment comment --></

  7. #6
    icepack's Avatar
    icepack is offline Programmer
    Join Date
    Jul 2006
    Location
    North Carolina
    Posts
    115
    Rep Power
    21
    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.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C Defining Data Arrays And Not BSS Arrays?
    By RhetoricalRuvim in forum C and C++
    Replies: 1
    Last Post: 09-02-2011, 07:59 AM
  2. Need Help on arrays and linking to arrays
    By Trendnet18 in forum Java Help
    Replies: 15
    Last Post: 01-19-2010, 11:56 AM
  3. Arrays
    By hoku2000_99 in forum C and C++
    Replies: 2
    Last Post: 03-24-2009, 09:23 AM
  4. Js, XML, and arrays
    By domestic in forum JavaScript and CSS
    Replies: 4
    Last Post: 02-27-2008, 02:49 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts