On Monday, 17 August 2015 at 02:45:22 UTC, Brandon Ragland wrote:
Howdy,

Since Dynamic Arrays / Slices are a D feature, using pointers to these has me a bit confused...

Consider:


Now what is especially confusing about this, is that the above seems to works fine, while this does not:

if(file[(*pos + i)] == '}'){
        *pos += i;
        return;
}

This fails with this error:
Error: incompatible types for ((file[cast(ulong)(*pos + i)]) == ('}')): 'char[]' and 'char'

This is wrong. what you want is (*file)[(*pos + i)] == '}'
file is a pointer. deref it you get a char array. index that you get a char.
Now what I do not understand, is if the above works, by appending come chars gathered from the dynamic array via pointer to me new dynamic array named "s", and the below does not seem to work on comparison of two chars, what is really going on?

I can no longer assume that using the dynamic array pointer works anything like a standard pointer to an array,

It is. arr.ptr[n] is the same as arr[n] (modulo bounds checking)

or a pointer to a dynamic array.

Is there something I'm missing?

Remember, that de-referencing a dynamic array was deprecated. So what I would normally have done: &dynamic_array_pntr does not work any longer, and there is no new spec on what to do...

-Brandon

string c2s(int* pos, char[]* file, int l){
        char[] s;
        for(int i = 0; i < l; i++){
                s ~= file[(*pos + i)];
        }
        return s.dup;
}


would more idiomatically be written as
string c2s(int* pos, char[]* file, int k)
{
    char* p = *file.ptr+*pos;
    return  p[0 .. k].dup
}

or changing the signature
string c2s(char[] file, size_t offset,size_t length)
{
    return file[offset .. offset + length].dup;
}

Reply via email to