Thread: Question on Arrays

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    3

    Question on Arrays

    Hello everybody! This is my first post in C board and I hope that someone can help me out. Just recently I have began learning how to program with some C Programming books. I have reached to the section where they began to talk about Arrays. I have no idea what they are talking about! I've looked at some simple practice progams using arrays and I am still confused. Can someone please help me explain exactly what arrays are? Or perhaps recommend some good tutorial sites reguarding about C would be awesome too.


    Thank You for you time and response.

    I hope this is the right section to post this in.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    An array is a consecutive list of items of the same type.
    Code:
    int array[10];
    An array has four pieces:
    The type of array. In this case, it's an array of integers.
    The name of the array. Just like every other variable instance, you have to give it a name.
    The size of the array. In this case, this array stores ten integers.
    The [ ] brackets, which wrap around the size. Think of them as a container. Thus, read backwards:

    "This array [ ] (contains) 10 integers."

    Think of each array member as a drawer in a cabinet. The drawers are numbered starting at zero and going through size - 1.

    Thus, our "drawers" are numbered 0, 1, 2, 3 ... 9.
    To access a "drawer", you do the following two methods (for beginners, we'll stick with these two):
    Code:
    int array[10];
    int a_number = 5;
    
    array[2] = a_number; /* put something into a drawer */
    a_number = array[2]; /* put what's in this drawer into a variable */
    In the first method, we say "Find drawer labled 2 and put a_number into it.
    In the second, we say "Find drawer labled 2, and put that into a_number".

    The key to remember is that arrays start at zero, and not one.

    [edit]Someone should FAQ this. I think Prelude has something on arrays, but it's dealing mainly with pointers, not array basics.[/edit]

    Quzah.
    Last edited by quzah; 06-05-2004 at 11:35 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you should always remember that with arrays
    first you create it with something like
    Code:
    int array[10];
    And you say to yourself. int array for 10 integers... or something like that. but always count with 0 through the number you want minus 1. Always important

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, I pretty much covered that. Or did all the colors confuse you?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    yeah. Just emphasizing

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by linuxdude
    yeah. Just emphasizing
    I'm just (censored)in' with ya.

    Quzah.

    [edit]
    self-censored, because the censor itself is well... not as amusing. On an aside, it's trivially easy to defete the censor. Like... ah, you figure it out.
    [/edit]
    Last edited by quzah; 06-06-2004 at 12:25 AM.
    Hope is the first step on the road to disappointment.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    and remember that just because the array starts at 0 doesnt mean you can declare
    Code:
    int array[10];
    and use 11 elements (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    the last element (10) is to be left NULL.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the last element (10) is to be left NULL.
    There is no index 10 in an array of 10 elements

    It is however legal to point to element one past the end of the array, but you can only use such a pointer in pointer comparisons (you can never dereference it).
    Code:
    int a[10];
    int *start = a, *end = &a[10];
    while ( start < end ) {
      // do stuff
      start++;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >the last element (10) is to be left NULL.
    No, the last element is to be left indeterminate and any access of the value is undefined. Also, C guarantees that &array[10] can be used as long as the address isn't dereferenced. That way you can do things like this:
    Code:
    int a[10];
    int *p;
    
    for (p = a; p != &a[10]; p++) {
      // Do da, do da
    }
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    3
    Thank you for all the responeses guys! Quzah your explaination helped me a lot. Now i have a much more clear understanding of arrays. Now to move on to matrices and pointers

    Some more things I need to be cleared up on.

    I liked the example that quzah gave about the drawers in a cabinet so I will stick with that.

    Would matrices would be a huge cabinet with many rows and columns of drawers?
    For example array [3] [2] would have 3 rows and 2 columns? And all those drawers would be just holders for the int. This would mean that it can contain a total of 6 integers right? Wouldn't array [6] be the same as array [3][2]?

    Also is a pointer like the person who is sorting out their clothes in specific drawers? For example I want to put socks in drawer [0][1]

    #include "stdafx.h"
    #include "stdio.h"

    int main(int argc, char* argv[])
    {
    int cabinet[3][2];
    int *socks;
    socks = &cabinet[0][1];
    printf("%d %d\n", socks, &cabinet[0][1]);

    return 0;
    }


    But when I run exe it the results is

    1245036 1245036

    I wanted it to read socks &cabinet[0][1]





    Thank you for your help!

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Would matrices would be a huge cabinet with many rows and columns of drawers?
    quzah's little abstraction kind of breaks down when you add more dimensions. A matrix is a two dimensional array. A two dimensional array is not really a two dimensional array, but a one dimensional array of one dimensional arrays. Using quzah's example, a cabinet where each drawer contains a cabinet.

    >But when I run exe it the results is
    >1245036 1245036
    That's correct (somewhat). You're asking printf to print the addresses of socks and &cabinet[0][1], which is what it's doing. However, to be strictly correct, the call to printf should look like this:
    Code:
    printf("%p %p\n", (void *)socks, (void *)&cabinet[0][1]);
    The %p format flag is used to print pointers and those pointers must be cast to void *.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM