Andrew2172 wrote: |
---|
So my teacher sucks at explaining things in class. |
I laughed :)
Andrew2172 wrote: |
---|
Can someone give me a basic run through that is easy to understand on these two points please? |
Sure. First: Arrays.
An array is a group of something, such as integers or characters. They are stored in memory -- one next to the other, with no spacing between them. Each object within the array is called an
element. Each elements has an
index. An index is a number that ranges from 0 (zero) to
n-1 (n is the length of the array). The reason 1 was deducted from n was because computers count from 0 (zero). Effectively, 0 (zero) becomes 1. For example, if I had an array of 10 elements, the index of the last element will be 9.
Declaring an Array
Declaring an array takes this form:
<Type> <Name>[<Size>]. Here,
<Type> is type of data the array will hold.
<Name> is obviously the name of the array.
<Size> is the amount of elements of
<Type> the array will hold. Here's a real-world example:
This array holds 5
ints. But how do you access the elements? It's straight forward. What you have to do is state the index of the element you want to access. For example, say you had an array of 10 elements, and you wanted the 4
th. You'd takes the element number and reduce it by 1; this will give you the index required to access the elements. Here's a working example:
1 2 3 4
|
int My_array[10] = { 0 };
// Access the array.
My_array[(4 - 1)];
|
In the above code, I've simplified it. First I declared an array. But what's the part after it? That's the
initialiser list. It's used to assign each element a value. However, when it's used in the same way as above, it means all elements of the array are set to 0 (zero). Then, I attempted to access the array. Normally, you would given the index directly. For this example, though, I've made it easier. When accessing an array, you're require to use the sub-script operator ([...]), coupled with an index. The 4 in this example is the element I want to access. I took 1 away from it to get the index of the 4
th element. C++ will then return the 4
th element of the array. There's two other ways to access an array, but I want state them here.
Pointers
A pointer is a variable, but a bit different from your average
int X variable. A pointer points to another previously defined variable. With that said, it's safe to say that the value of a pointer is the address of another variable.
A pointer takes this form:
<Type> *<Name>. Here,
<Type> is the type of variable the pointer will point to.
<Name> is obviously the name of the pointer.
C++ is quite strict with pointers and the objects they point to. When assigning a pointer to a variable, you have to explicitly pass it the variable's address. An address is the location in memory where the variable resides. This is usually represented in hexadecimal. Here's an example:
1 2
|
int My_variable(0);
int *My_pointer(&My_variable);
|
In this code, I first declared a basic
int variable. I then created a pointer. To initialised my pointer, I had to give the pointer an address to point to. I obtained the address with the
address-of operator. This operator takes the form of the ampersand (&). The address-of operator is an unary operator (an operator that takes only 1 operand). Its job it to return the address of its operand. When the pointer is given this address, it points to it.
Note the funky use of parentheses. This is called
functional notation. It's equivalent to:
1 2
|
int My_variable = 0;
int *My_pointer = &My_variable;
|
Now, pointers aren't treated the same way as variables. Instead, they require dereferencing with the asterisk (*). This is the term used to ask the pointer what it's pointing to. Dereferencing can be worded as:
"Pointer, what are you pointing to?". A dereferencing operation returns the object it points to. Still using the code above:
This assignment statement does 2 things:
1) The compiler sees the asterisk and realises that you want the object pointed-to by
My_pointer. This operations returns
My_variable because
My_pointer was set to point to it initially.
2) With point 1 in mind, the compiler assigns
My_variable the value of 10. Don't forget that
My_pointer is pointing to
My_variable.
This is the best I can do while in my current condition -- I'm kinda drunk, you know, from a night out.
Wazzak