Arrays are confusing...

This is a discussion on Arrays are confusing... within the C Programming forums, part of the General Programming Boards category; Hey, I'm currently using howstuffworks.com's C tutorial because the one on this site was confusing. Anyway, I am completely lost ...

  1. #1
    The Hawk Man
    Join Date
    Nov 2005
    Location
    Beaverton, OR USA
    Posts
    13

    Arrays are confusing...

    Hey, I'm currently using howstuffworks.com's C tutorial because the one on this site was confusing. Anyway, I am completely lost when it comes to arrays. I just don't understand them. If someone could explain them and give examples or point me to a tutorial that explains them well, I'd really appreciate it. Thanks.

    -Gavin

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    An array is a number of elements placed one after the other.
    you can declare a single charactor like this
    Code:
    char c;
    but you might want to hold serveral charactors and for that wa can use an array
    Code:
    char c[10];
    That declares an array of 10 charactors numbered 0-9, we can access anyone like this
    Code:
    a=c[x];
    Where x is the number of the element from 0-9 that we want to access. x can also be a variable which we can increment/decrement to get different elements.

  3. #3
    The Hawk Man
    Join Date
    Nov 2005
    Location
    Beaverton, OR USA
    Posts
    13
    Well that was somewhat less confusing. LOL Thanks, though.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    34
    Did it help you understand or are you still confused? An array basically will let you keep several variables of the same type under the same name. So instead of doing something like this ..

    Code:
    int i1, i2, i3, i4, i5;
    
    i1 = 10;
    i2 = 20;
    i3 = 30;
    i4 = 40;
    i5 = 50;
    You could simplify it and do something like this ..

    Code:
    int i[5];
    
    i[0] = 10;
    i[1] = 20;
    i[2] = 30;
    i[3] = 40;
    i[4] = 50;
    Remeber that C starts counting at 0, not 1. So if you used the above code and tried to access i[5] you would have a problem because it doesn't actually exist. Of course you could change that to use a for loop to make it a lot easier, that was just an example. If you're still confused I'm sure I can throw a better example together.
    -gunder

    if (problem)
    postcount++;

  5. #5
    The Hawk Man
    Join Date
    Nov 2005
    Location
    Beaverton, OR USA
    Posts
    13
    Yes, I'm confused because what if I want to use a word as the value instead of a number? For example, I have a program that uses if statements to generate a zodiac sign based on the users input of birth month (i.e. 2) and day (i.e. 26).

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Is this what you want ??
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
       char zodiac_signs[][12] = { "ARIES",
                                   "TAURUS",
                                   "GEMINI",
                                   "CANCER",
                                   "LEO", 
                                   "VIRGO",
                                   "LIBRA",
                                   "SCORPIO",
                                   "SAGITTARIUS",
                                   "CAPRICORN",
                                   "AQUARIUS",
                                   "PISCES" };
       char zodiac_dates[][20] = { "Mar. 21 - Apr. 20",
                                 "Apr. 21 - May  21",
                                 "May  22 - June 21",
                                 "June 22 - July 22",
                                 "July 23 - Aug. 22",
                                 "Aug. 23 - Sept 23",
                                 "Sept.24 - Oct. 23",
                                 "Oct. 24 - Nov. 22",
                                 "Nov. 23 - Dec. 21",
                                 "Dec. 22 - Jan. 20",
                                 "Jan. 21 - Feb. 19",
                                 "Feb. 20 - Mar. 20" };
       int i;
       for ( i= 0; i < 12; ++i ) {
          printf( "%-15s %s\n", zodiac_signs[i], zodiac_dates[i] );
       }
       return 0;
    }
    Kurt

  7. #7
    The Hawk Man
    Join Date
    Nov 2005
    Location
    Beaverton, OR USA
    Posts
    13
    I don't understand that code (like how it works, exactly) but I did put it into my compiler and it works. I just don't quite understand how. I'm starting to get frustrated. *goes off to find more tutorials*
    Last edited by GavinHawk; 11-28-2005 at 10:35 PM.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    What ZuK made was a character array, or a C-style string if you will. Essentially, an array is a block of memory equal to the size of the datatype times the size of the array. So if I made an array of 10 characters, the program would reserve a space for 10 characters(10 bytes).

    Code:
    char string[10]; // This reserved the bytes in succession
    Since they're in succession, the program can very easily find each element by going to the first element 'string' and counting down from there. So to retrieve the value of string[5], the program will go to the beginning of the memory and count five down.

    Now to go into more detail of what ZuK did, he created a two-dimensional array. The first bracket being the length of the string(which he left blank, but since it's initalized, the size is created as the largest initalized value, which I believe was "SAGITTARIUS") and the second bracket is the number of arrays you want. Try to visualize it like this.

    Code:
    zodiac_signs          First Bracket
                 0  1  2  3  4  5  6  7  8  9  10  11
               1[A][R][I][E][S][\0][ ][ ][ ][ ][ ][ ]
         S     2[T][A][U][R][U][S][\0][ ][ ][ ][ ][ ]
         c     3[G][E][M][I][N][I][\0][ ][ ][ ][ ][ ]
         d.    4[C][A][N][C][E][R][\0][ ][ ][ ][ ][ ]
               5[L][E][O][\0][ ][ ][ ][ ][ ][ ][ ][ ]
         B     6[V][I][R][G][O][\0][ ][ ][ ][ ][ ][ ]
         r     7[L][I][B][R][A][\0][ ][ ][ ][ ][ ][ ]
         a     8[S][C][O][R][P][I][O][\0][ ][ ][ ][ ]
         c     9[S][A][G][I][T][T][A][R][I][U][S][\0]
         k    10[C][A][P][R][I][C][O][R][N][\0][ ][ ]
         e    11[A][Q][U][A][R][I][U][S][\0][ ][ ][ ]
         t    12[P][I][S][C][E][S][\0][ ][ ][ ][ ][ ]
    If you don't quite understand this, try a few examples on your compiler. One thing to remember with arrays though is you should never go out of the bounds of it's memory. If you set the size to 10, then don't try to access index 10 otherwise your program may crash.
    Last edited by SlyMaelstrom; 11-28-2005 at 11:30 PM.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    34
    That's an awsome little diagram SlyMaelstrom. GavinHawk I went looking for a tutorial that might help you, this one seemed to explain it fairly clearly. Try a few tests like what SlyMaelstrom suggested, if you still don't quiet get it just ask, it seems the people here are more than willing to help.

    http://cplus.about.com/od/beginnerct.../aa040802a.htm
    -gunder

    if (problem)
    postcount++;

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by SlyMaelstrom
    Now to go into more detail of what ZuK did, he created a two-dimensional array. The first bracket being the length of the string(which he left blank, but since it's initalized, the size is created as the largest initalized value, which I believe was "SAGITTARIUS") and the second bracket is the number of arrays you want. Try to visualize it like this.

    Code:
    zodiac_signs          First Bracket
                 0  1  2  3  4  5  6  7  8  9  10  11
               1[A][R][I][E][S][\0][ ][ ][ ][ ][ ][ ]
         S     2[T][A][U][R][U][S][\0][ ][ ][ ][ ][ ]
         c     3[G][E][M][I][N][I][\0][ ][ ][ ][ ][ ]
         d.    4[C][A][N][C][E][R][\0][ ][ ][ ][ ][ ]
               5[L][E][O][\0][ ][ ][ ][ ][ ][ ][ ][ ]
         B     6[V][I][R][G][O][\0][ ][ ][ ][ ][ ][ ]
         r     7[L][I][B][R][A][\0][ ][ ][ ][ ][ ][ ]
         a     8[S][C][O][R][P][I][O][\0][ ][ ][ ][ ]
         c     9[S][A][G][I][T][T][A][R][I][U][S][\0]
         k    10[C][A][P][R][I][C][O][R][N][\0][ ][ ]
         e    11[A][Q][U][A][R][I][U][S][\0][ ][ ][ ]
         t    12[P][I][S][C][E][S][\0][ ][ ][ ][ ][ ]
    Actually you have it the wrong way around.

    When you do:
    Code:
    char x[][10] = { "foo", "bar", "baz", "quux" };
    You are saying that each string has a maximum size of 10 (edit), and you don't need to specifiy the first value because the compiler can count the number of entries you gave. The declaration above is effectively char x[4][10], because there are four strings.. The strings can take up upto 10 characters each (or 9 not including the terminator).


    Thus, in your diagram, the first paramater (bracket) gives you the string number, and the second the character offset in the string. The 12 in your zodiac declaration is the maximum string length. It coincidentally is the same as the number of months/zodiac signs.
    Last edited by cwr; 11-29-2005 at 12:18 AM.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Yeah that's right, sorry I had it backwards. Now thinking about just a standard array of:

    Code:
    int numbers[] = {0,1,2,3,4};
    That does make sense.
    Last edited by SlyMaelstrom; 11-29-2005 at 12:12 AM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21