相关疑难解决方法(0)

如何在c ++中创建连续的2d数组?

我想创建一个在C++中返回连续2D数组的函数.

使用以下命令创建阵列不是问题:

 int (*v)[cols] = new (int[rows][cols]);
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何将此数组作为函数的常规类型返回.功能是:

  NOT_SURE_WHAT_TYPE create_array(int rows, int cols)
  {
        int (*v)[cols] = new (int[rows][cols]);
        return v;
  }
Run Code Online (Sandbox Code Playgroud)

我尝试了双*[]和双**,两者都不起作用.我不想使用double*,因为我想从外部访问这个数组作为2D数组.

相关问题:如何使用new在C++中声明二维数组?

c++

10
推荐指数
5
解决办法
6206
查看次数

如何将字符串数组从C和Fortran传递到Fortran?

我试图将字符串数组从C传递到Fortran子例程,以及从Fortran传递到相同的Fortran子例程。我已经成功地从C和Fortran中成功传递了单个字符串(即1D字符数组)。但是,我在处理字符串数组时遇到了麻烦。我在Fortran端使用ISO C绑定,理想情况下,我希望在调用端尽可能做到无缝。

我已经阅读了一些相关的问题和答案。有些(即thisthis)只是“使用ISO C”而没有更多细节,这并没有太大帮助。这个答案非常有帮助(对不同问题的类似答案),但是仅适用于单个字符串,其中似乎在单个Fortran字符串中识别了c_null_char。如果没有两个单独的例程,我无法弄清楚数组情况该怎么办。

我现在有一个C例程,我想从其中传递字符串数组(string):

#include <iostream>

extern "C" void print_hi_array(char input_string[][255]);

using namespace std;

int main() {

  char string[3][255] = {"asdf","ghji","zxcv"};   
  print_hi_array(string);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

并且,一个类似的Fortran例程:

program main
  implicit none
  call print_hi_array( (/"asdf", "ghji", "zxcv"/) )
end program
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我对接收端的要求:

subroutine print_hi_array(input_string) bind(C)
  use iso_c_binding, only: C_CHAR, c_null_char

  implicit none

  character (kind=c_char, len=1), dimension (3,255), intent (in) :: input_string
  character (len=255), dimension (3) :: regular_string
  character (len=255) :: dummy_string
  integer …
Run Code Online (Sandbox Code Playgroud)

c c++ fortran fortran-iso-c-binding

5
推荐指数
1
解决办法
2873
查看次数

标签 统计

c++ ×2

c ×1

fortran ×1

fortran-iso-c-binding ×1