Indexing in raw pointer

fn main(){
let width= 10_usize;
let hight = 100_usize;
let mut grid_raw = vec![0 as f64; hight * width];
//create one vec on some pieces
let mut chunk_on_width: Vec<f64> = grid_raw.as_mut_slice().chunks_mut(width).collect();
let chunk_on_width: &mut [&mut [_]] = addition_mccorn_array.as_mut_slice();
let mut prediction: *mut [f64] = chunk_on_width[0];
//... *elsewhere* parts of two dim array and then i want to index in one
 prediction[k] =  5_f64;

ERROR: error[E0608]: cannot index into a value of type *mut [f64]
Also if i want to pass it in c function , for example with signature

int cfunc( double *Fi, double * Ftd)

will be error of type:
= note: expected raw pointer *mut f64
found raw pointer *mut [f64]
As i want only pair of additional arrays to pass in,

I don't really understand what's going on here. Everything is mixed together. Can you please post the error and code separately? The way you index into an *mut [f64] is with (*ptr)[idx]

1 Like

*ptr it is from std:: or usual deref?
So what i need to write: (*prediction)[k]?
In std i'm interesting what is usual way to wrap raw pointers?

I just used ptr as a variable name. It's not a module, no. And yes, it is (*prediction)[k].

What do you mean about wrapping them?

1 Like

I mean it's very confusing to manage with raw pointers, in c++ it will be massive with which it is easy to manage unlike in rust fix sized array with which you can't do anything, but i think in rust it is some wraps about this and some keys to keep it safe)

I think you are looking for the as_ptr() and as_mut_ptr() methods on slices to get an immutable/mutable pointer to the first element.

let buffer: &[i32] = &[1, 2, 3, 4];

unsafe {
    // normal index syntax on the slice
    assert_eq!(buffer[2], 3);
    // using pointer arithmetic
    let ptr = buffer.as_ptr();
    let third_element: *const i32 = ptr.add(2);
    assert_eq!(*third_element, 3);
1 Like