source
is declared as
pointer to const unsigned int, then
pointer arithmetic is used on it. To understand how it works let's have a look to the code snippet below:
unsigned int data[1024];
for(int i = 0; i < 1024; i++) data[i] = i;
const unsigned int *source = data;
printf("*source is %d\n", *source);
source++;
printf("*source is %d\n";, *source);
source += 10;
printf("*source is %d\n", *source);
the output produced is:
*source is 0
*source is 1
*source is 11
this because:
- before the first
printf
statement the starting address of data
is assigned to source
, then it points to the first element of the array, data[0]
- after that, the statement
source++
increments the pointer by one, and this means: increase the pointer so that it points to the next item of the type it points to (i.e. the pointer is increased by 4 because this is the size of an unsigned int). Then the second printf
shows the value of data[1]
- finally, the statement
source += 10
increase the pointer by the amount of memory occupied by 10 unsigned int, thus it points to data[11]
Note that there is not an equivalent to
pointers and
pointer arithmetic in
C# as long as you don't use
unsafe code.
However the use of
unsafe code is not recommended, and the best option while translating
C++ code to
C# code is to rewrite from scratch those parts that use pointers to avoid it.
If your target is to reuse your
C++ code under
.NET, think about using
C++/CLR which is the best choice for
legacy code (it allow to mix
managed and
unmanaged code inside an assembly, so you can use it to interface old native projects to new
.NET projects written in any language that supports it).