Lesson 7
OMG U R STIL HEER?!
Alright yesterday we covered pointers, I hope you got them, or at least some idea of how they work. We are going to move on to Arrays now, Arrays are a special kind of pointer, there isn't anything paticularily special about them except that they can be used to reserve spots for multiple variables right next to each other. All an array variable really contains is an address, but at that address we will have a certain number of variables, for instance we could make an array of 10, 20 even 100 ints or more. 1MB of ints would be over 262 thousand of them so there really isn't a limit on the number of elements in an array. Lets take a look at an Array declaration:
int array[4]; //first the type, then the name, and then the number of elements
pretty simple, you should keep in mind that the number of elements must be a constant so no variables in there. Arrays can be used to store more complex information that just basic numbers or letters, One of the most common uses of arrays is for storing strings. Strings are arrays of characters, we have actually already worked with strings in the form of constant strings (anything with "" around it is a constant string).
Ok so now you know how to declare arrays, but you don't know how to define them, the notation for it is pretty simple, we actually are going to use a new operator, they are officially called the array operator, they work by changing a pointer (remember arrays are just pointers) to an int like the indirection operator, but it also includes an offset value, lets take a look at a full declaration and definition of an array of integers:
Alright well I hope you have a good handle on how the array operator works, and realize that an array really is just a pointer and that the two really are interchangable.In fact we could even assign a_pointer to point to the first element of array, and we could use a_pointer to do everything we do to array. Now some of the more astute of you may have noticed something else here, and that is that the left side of the equation would have to evaluate before the right for my definitions to work. And in this case that is true, just like in math you have an order of operations, in programming there is an order of precedence, here is an aproximation of that order:
() parentheses just like in math whatever is in these is done first
() gah again!? actually these are for functions,the parentheses of a function call
[] pretty high precedence for our array operator
++ -- these are the postfix notations though
++ -- these are the prefix
+ - this is actually for negative or positive
! the good old not operator
& ah the address operator
* then the indirection operator
*/ multiply and divide
+ - addition or subtraction
== != equal or not equal
&& logical and
|| logical or
then the assignment operators, and finally the comma.
These are not all the operators but they are all the ones we've talked about
Ok well I'm sorry I know I've been slacking the last few days, just been a little stressed and I apologize, hopefully tomorrow I'll be back on my game, until then try experimenting with different array types, we are going to get into arrays alot more in the next lesson, and we are going to learn our first preprocessor directive besides #include!!
OMG U R STIL HEER?!
Alright yesterday we covered pointers, I hope you got them, or at least some idea of how they work. We are going to move on to Arrays now, Arrays are a special kind of pointer, there isn't anything paticularily special about them except that they can be used to reserve spots for multiple variables right next to each other. All an array variable really contains is an address, but at that address we will have a certain number of variables, for instance we could make an array of 10, 20 even 100 ints or more. 1MB of ints would be over 262 thousand of them so there really isn't a limit on the number of elements in an array. Lets take a look at an Array declaration:
int array[4]; //first the type, then the name, and then the number of elements
pretty simple, you should keep in mind that the number of elements must be a constant so no variables in there. Arrays can be used to store more complex information that just basic numbers or letters, One of the most common uses of arrays is for storing strings. Strings are arrays of characters, we have actually already worked with strings in the form of constant strings (anything with "" around it is a constant string).
Ok so now you know how to declare arrays, but you don't know how to define them, the notation for it is pretty simple, we actually are going to use a new operator, they are officially called the array operator, they work by changing a pointer (remember arrays are just pointers) to an int like the indirection operator, but it also includes an offset value, lets take a look at a full declaration and definition of an array of integers:
PHP:
#include iostream.h
int main ()
{
int an_int = 3;
int* a_pointer = &an_int;
int an_array[3];
array[0] = 1; //Ok so we have zero offset so this is the first element
array[1] = 2; //one offset this is the second element
array[2] = 3; //finally we have two offsets or the third elment
cout << array[0]; //here we are accessing the first element again
cout << "\n"; //hey look a string!
cout << array[1]; //here we access the second element
cout << "\n";
cout << array[2]; //and the third
cout << "\n";
cout << a_pointer[0]; //and here we use the array operator on GASP a pointer!
return 0;
}
Alright well I hope you have a good handle on how the array operator works, and realize that an array really is just a pointer and that the two really are interchangable.In fact we could even assign a_pointer to point to the first element of array, and we could use a_pointer to do everything we do to array. Now some of the more astute of you may have noticed something else here, and that is that the left side of the equation would have to evaluate before the right for my definitions to work. And in this case that is true, just like in math you have an order of operations, in programming there is an order of precedence, here is an aproximation of that order:
() parentheses just like in math whatever is in these is done first
() gah again!? actually these are for functions,the parentheses of a function call
[] pretty high precedence for our array operator
++ -- these are the postfix notations though
++ -- these are the prefix
+ - this is actually for negative or positive
! the good old not operator
& ah the address operator
* then the indirection operator
*/ multiply and divide
+ - addition or subtraction
== != equal or not equal
&& logical and
|| logical or
then the assignment operators, and finally the comma.
These are not all the operators but they are all the ones we've talked about
Ok well I'm sorry I know I've been slacking the last few days, just been a little stressed and I apologize, hopefully tomorrow I'll be back on my game, until then try experimenting with different array types, we are going to get into arrays alot more in the next lesson, and we are going to learn our first preprocessor directive besides #include!!