# Arrays are confusing...

• 11-25-2005
GavinHawk
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
• 11-25-2005
Quantum1024
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.
• 11-25-2005
GavinHawk
Well that was somewhat less confusing. LOL Thanks, though. :)
• 11-25-2005
gunder
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.
• 11-26-2005
GavinHawk
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).
• 11-26-2005
ZuK
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
• 11-28-2005
GavinHawk
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*
• 11-28-2005
SlyMaelstrom
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.
• 11-28-2005
gunder
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. :)

• 11-28-2005
cwr
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.
• 11-29-2005
SlyMaelstrom
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.