Came across this codes... have no idea what does the * mean... any idea??
Example1: byte
* ptr = (byte *) imageData.Scan0.ToPointer( ); byte * ovr = (byte *) ovrData.Scan0.ToPointer( );
Example2: v = (int) *ptr + (int) *ovr;
*ptr = ( v > 255 ) ? (byte) 255 : (byte) v;
AlanPosted Apr 20, 2008, 5:46 AM
* denotes a pointer to where a value is stored in memory rather than the value itself.
So, in the line of code below, 'ptr' is a variable of type 'pointer to byte' and the pointer returned by the expression imageData.Scan0.ToPointer() is being cast to a byte pointer before being assigned to the variable.
byte * ptr = (byte *) imageData.Scan0.ToPointer( );
As well as denoting pointer types, the * operator can also be used to 'dereference' a pointer varable. It then precedes the name of the variable it is dereferencing. Dereferencing a pointer means to obtain the value to which it points. So, in the following line, the two byte pointer variables 'ptr' and 'ovr' are being dereferenced to obtain the byte values to which they point and these values are then being cast to int, added together and assigned to the variable 'v'.
v = (int) *ptr + (int) *ovr;
In unmanaged languages such as C/C++, the use of pointers is commonplace. There is little need for them in a managed language such as C# and they can only be used in what's called an 'unsafe' context which means, basically, a block of code preceded by the 'unsafe' keyword. This is to ensure that pointers are not used by accident in a program.
However, they are sometimes needed when hardware or system software is being accessed directly, when interfacing with unmanaged code which uses pointers or when trying to speed up an algorithm as much as possible.