# Question on Arrays

This is a discussion on Question on Arrays within the C Programming forums, part of the General Programming Boards category; Hello everybody! This is my first post in C board and I hope that someone can help me out. Just ...

1. ## 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. 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.

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

Quzah.

3. 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. Yeah, I pretty much covered that. Or did all the colors confuse you?

Quzah.

5. yeah. Just emphasizing

6. Originally Posted by linuxdude
yeah. Just emphasizing
I'm just (censored)in' with ya.

Quzah.

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]

7. 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. > 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++;
}```

9. >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
}```

10. 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]

`printf("%p %p\n", (void *)socks, (void *)&cabinet[0][1]);`