A lot of the comments you're getting are happening because your C++ code is nonsensical, and people don't understand why you would want to do that, in C++ or Delphi.
A pointer points to a memory address, which is generally assumed to contain a variable. But in C (and C++ and similar languages) a pointer is also used as an array; instead of a well-defined "array type", you simply have a pointer to the first element and array syntax that works with it. Your buffer+=1
line basically means "move the pointer to the variable element after the one it's currently at." You claim that now buffer[0] has value of buffer[1] and buffer[1] has value 0
, but it's not quite that simple. The old buffer[0] didn't go anywhere; you just moved the pointer.
You went from:
|
V
-----------------
| 0 | 1 | beyond|
-----------------
|0.1|0.2|???????|
-----------------
to:
|
V
-----------------
| 0 | 1 | beyond|
-----------------
|0.1|0.2|???????|
-----------------
It now looks like buffer[0] has changed its value, but the values haven't changed; you just moved the pointer.
And I left the values after the end of the array marked as ?s for a reason. That's not really a 0; that's undefined, and it contains whatever happens to be in memory at those bytes. It could be a 0 now, but if you run it again (or run it on a different computer) you could easily get some random number. "Undefined behavior" is programmer-speak for "things go wrong in unpredictable and hard-to-track-down ways." It's not a good thing. C is full of it; Delphi, much less so.
The reason what you're trying to do doesn't work in Delphi is that Delphi has well-defined array types that are completely different from pointers. A dynamic array may look like "just an array" but it's actually a data structure containing metadata about the array, with an array glued on the end of it. So you can't move the pointer around; there is no "element pointer" the way there is with C code.
If you really want to get a pointer to an element in an array, perhaps because you're scanning the array one element at a time, you declare a variable of a pointer type (with the ^
operator) and set it with the @
operator, like so:
var
buffer: array of Single;
ptr: ^single;
begin
SetLength(buffer,2);
buffer[0]:=0.1;
buffer[1]:=0.2;
ptr := @buffer[1];
end;
But again, a pointer is not an array, an array is not a pointer. So you would not be able to say ptr[0]
. (A few pointer types, such as PChar
, make an exception to this rule for practicality reasons because they represent C API strings, but as a general rule you should not treat pointers like arrays.)
And if what you want to do is scan through an array, there's a better way. Because the array type is well defined and Delphi knows how long it is, you can do this:
for value in buffer do
//do whatever
This is called an enumerator, and it acts as a special kind of for-loop that loops over each element in the array, feeding it into the index variable.
I hope this helps. If not, please clarify what you're trying to do with this code and I'll try and explain better. :)