L34rn teh C++ Lesson 7

Cunbelin

Newbie
Joined
Aug 3, 2003
Messages
281
Reaction score
0
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:

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!!
 
nice tutorial ;) although im still working on lesson 6..trying to understand the concept. do you mind if i put your tutorials on my homepage as soon as hl2 comes out? you get the credit, of course. i'm looking forward for the preprocessor directives ;)
 
i just got the concept of pointers and used them in differnet functions, thx cunbelin ;) .. let'S go for lesson 7 :D

sry for double post: stupid limit.
 
once again..the limit.

i spotted an mistake in the array.

you have defined the array: an_array[3]
but you use: array[1] = ...
 
once again..the limit.

i spotted an mistake in the array.

you have defined the array: an_array[3]
but you use: array[1] = ...

<edit> spaaaammmmm, why can't i delete messages within the 15 minutes time limit?</edit>
 
I apologize guys for not getting on to lesson 8, my monitor died on me, but I've managed to get a loaner up and running, though it barely works. I'm sorry about my mistake with the an_array array problem, I was probably pulling from different code bases and screwed up there. There has been a request to centralize these somewhere, I'll see what I can do, for now though I'm going to try to keep posting here on the forums. Again sorry for my absence, I'll get to work on getting Lesson 8 up and ready.
 
/me is waiting for lesson 8 ;). hope you'll be back soon for teaching us cool things. i honestly never understood why to use pointers. but altering variables in functions which are out of focus is a nice thing :D
 
Sorry I've hit a snag with centralizing things, but hopefully I'll have something up Sunday.
 
I know I'm a bit fussy here, but the the iostream.h header is depricated. You should use
PHP:
#include <iostream>
using namespace std;
 
he also can use
PHP:
#include <iostream.h>
;)

the <> tell the compiler that the header file is one of the "official" (?) header files, not one written on your own. so it looks into another folder.

but the namespace thingy is also cool, i use it, too.
 
Originally posted by Echelon
he also can use
PHP:
#include <iostream.h>
;)

the <> tell the compiler that the header file is one of the "official" (?) header files, not one written on your own. so it looks into another folder.

but the namespace thingy is also cool, i use it, too.

#include <iostream>
using namespace std;

^^ that is the new standard for i/o. the old headers have been deprecated.
 
preprocessor directives?

well. some of you may have seen the following at the beginning of header files:

PHP:
#ifndef THISHEADERFILE_H
#define THISHEADERFILE_H

and then at the end:

PHP:
#endif


this code prevents the header from being included more than once. very useful.

the problem with it is that most people who use it don't have any idea why it works. allow me to explain. '#' symbols are used for what are called 'preprocessor directives'. these directives are not code that will be compiled. they are read by the compiler prior to compilation.

in this case, we used #ifndef ("if not defined"). this means the compiler will check if the variable following it has been #defined. if it has NOT ("if NOT defined"), then the compiler will proceed to compile all of the code between #ifndef and #endif. the next command you see following #ifndef is #define. psuedocode as follows.

PHP:
if variable HEADERNAME_H has not been defined:
        define variable HEADERNAME_H
        compile code until #endif
else
        skip to #endif

this is a very simple way to force the compiler only to compile each header file once.

in my program i'm working on right now, i have a #define WINDOWED_MODE at the beginning of the program. later, during screen initialization and such, there are several #ifdef's ("if defined")

PHP:
#ifdef WINDOWED_MODE
      // lots of windowed initialization here
#endif

#ifndef WINDOWED_MODE
      // lots of fullscreen initialization here
#endif

NOTE: notice that unlike an if... else statement, where the code is still compiled even if the statement returns false, the code between a failed #ifdef or #ifndef is NOT COMPILED. it's not in the program whatsoever.
 
Good topic to post on. :) I'll continue with it...

You can also use the preprocessor for some more advanced things... such as macro type stuff. The preprocessor can basically just work like a subsitution command. For instance:

PHP:
#define SOME_VALUE_USED_A_LOT 5

// somewhere else...
int x = SOME_VALUE_USED_A_LOT;
The preprocessor will go through your file and replace every instance of SOME_VALUE_USED_A_LOT with the text defined after it (5, in this case) before it sends it to the compiler itself. Note that it doesn't know anything about syntax so you have to be careful... it's just a find/replace.

And like I said... macro type stuff. This is a bad example, but it shows how you can treat #define's like a function:

PHP:
#define SOME_SHORTCUT( a, b ) a = b

// somewhere else...
int x = 0;
int y = 5;
SOME_SHORTCUT( x, y );
x is now equal to 5.

But remember: the preprocessor isn't intelligent. So be careful how you do things. It's actually just going in and replacing code. For example, let's say you had this (another bad example):

PHP:
#define SOMETHING( a, b, c, d ) (a = d, b = d, c = d)

// somewhere else:
int x, y, z, w;
w = 0;
SOMETHING( x, y, z, w++ );
Now, if this was a normal function, you would expect x, y, and z to be 0 and w to be 1 after calling this. But since it's a find/replace, you'll end up with x == 0, y == 1, z == 2 and w == 3. How? After the find/replace it would look like this:

PHP:
int x, y, z, w;
w = 0;
x = w++; // w == 1
y = w++; // w == 2
z = w++; // w == 3
Watch your code for tricky cases like this.
 
Back
Top