Hi everyone, I am trying to initialize a structure object of Cpp inside the rust by passing struct object from Cpp to Rust by pointer the desired output is coming when I am changing the definition of struct object array2d inside cpp, But I cannot change the definition.
I only have the flexibility to do anything in Rust. I have also tried taking ARRAY as a normal variable inside Rust and then initializing but it doesn’t work as well.
//file: ffi.h
#pragma once
struct dummy{
public:
int a;
int (*array2d)[4];
};
extern "C"
{
void func1(dummy *x);
}
//file: ___.cpp
#include<stdio.h>
#include<iostream>
#include "ffi.h"
using namespace std;
int main(){
dummy x;
x.a=1;
func1(&x);
cout<<x.a<<endl;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
cout<<x.array2d[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
//file: main.rs
pub static ARRAY:[[i32;4];4]= [[1,2,3,4],
[1,2,3,4],
[1,2,3,4],
[1,2,3,4]];
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[repr(C)]
pub struct dummy{
pub a:i32,
pub array2d:[[i32;4];4],
}
#[no_mangle]
pub extern "C" fn func1(x:&mut dummy){
(*x).a=10;
(*x).array2d=ARRAY;
println!("inside rust");
}
The output is:
inside rust
10
Segmentation fault (core dumped)
If i change
int (*array2d)[4]; → int array2d[4][4]
It is working fine and the output is
inside rust
10
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4