zylesea wrote:
@ bloodline
As an occasional programmer I again and again get into messes and troubles (at least when a ointer refers to a pointer and such complicated things) with pointers and references. I guess I know the logic behind it, but w/o regular practise it is often hard. But still from those three languages (C++, Matlab and Holywood) I seriously looked into I like it somehow most.
Anyway, I wanted to take the opportunity to advertise for Hollywood: Simple, but also powerful and write once, compile for MorphOS, AOS3.x, AOS4.x, Windows, OS X.
Honestly Pointers aren't really as bad as people make out... just think of a variable as a variable, just the same as in BASIC... then a pointer is the actual memory address of that variable and its datatype.
With a variable in any programming language, it has three properties... Its location in the computer (the address), its size (the datatype) and it's value (the data stored there).
A pointer is the first two properties, a variable is the second two properties.
when you define:
int A=5;
You know two things about it, the datatype (size), which is an integer (on Amiga that is 32bits) and you know its value (5).
To get the address you need to put an "&" in front of the variable... now you need to store that address, this is where the pointer comes in... since your variable is an interger, your pointer needs to know the size that it's pointing to thus:
int* B = A;
Has now stored the address and size of variable A into B.
Remember:
int is an integer variable.
int* is a pointer to an integer variable (it doesn't actually have to point to a variable, but that is another topic :-) ).
Now we can do thing a bit clever using the "*" operator... if you put the *
in front of a pointer then all operations happen to the location pointed to by the pointer, thus:
*B=6;
A=6;
both of the above assignments do the same thing. Both assign the value 6 to the variable A. But really you can ignore pointers to begin with... they are only really needed for doing advanced stuff that you can't do with BASIC.
The big advantage of pointers is that they allow a function to act on the original variable rather than on of copy of the variable... thus the function doesn't need to return any value, and no data needs to be copied, making the whole process much faster. I have no idea how I could survive without pointers now :-D (I'm not looking forward to trying C#)...
But if you come from BASIC, function/procedure support is often very limited and you can just write your C code all inline (ie in the main function).