Fortran 77 可变大小字符串数组?

发布于 2024-12-04 05:50:43 字数 270 浏览 2 评论 0原文

我是 Fortran 77 中的傻瓜,一直是 C++ 编码员,但我必须修改多年前的代码...... 我想创建一个可变大小的字符串数组,但我无法在网上找到如何在 Fortran 77 中执行此操作。 理想情况下,它应该是两个维度都可变的数组,但如果不可能,我可以修复字符串的长度,但我需要有可变数量的字符串。

我尝试过:

CHARACTER*32 STR1*(VAR1)
...
WRITE(6,*) STR1(10)

但这不起作用......

I am a dummy in Fortran 77 and have always been a C++ coder, but I have to modify a code from years long ago...
I want to create a variable size array of strings and I cannot find online how to do this in Fortran 77.
Ideally, it should be array with both dimensions variable but if it is not possible, length of the string I can fix, but I need to have variable number of strings.

I tried this:

CHARACTER*32 STR1*(VAR1)
...
WRITE(6,*) STR1(10)

But this does not work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2024-12-11 05:50:43

显然FORTRAN 77不支持动态内存分配。
您可以尝试在 ac 程序中分配内存并将结果传递回 FORTRAN 例程。

正如这里所做的那样
http://owen.sj.ca.us/~rk /howto/FandC/FandC.mem.html

或者更清楚 - 使用某种 Interop 来调用 ac 方法

来自 Fortran。

Unix 可移植 Fortran-77 编译器('f77')已经写得差不多了
完全用 C 编写。编译器的第二遍与
C 编译器和大多数“f77”库例程只是接口
到相应的 C 库例程。然而,由于 Fortran 不
支持像 C 中使用的数据结构,你可能无法
利用“curses”库的所有功能
优惠。用“curses”操作窗口尤其困难。如果
您对使用涉及数据结构的例程感兴趣,您
也许应该使用 C 而不是 Fortran。

要从 Fortran 程序调用 C 例程,您必须编写一些
C代码。 Fortran 通过引用或地址传递参数,因此 C
函数必须准备好接受变量作为地址。这
意味着你必须用 C 语言编写函数来调用
Fortran 在调用库之前正确设置参数
功能。从原理上讲,这可能类似于:

注意例程名称中的下划线 (_)。

在 C 源文件中:

foo_(bar) /* 注意下划线 */ int bar; / 注意变量是
通过地址传递 */

{:}

在 Fortran 源文件中:

call foo(baz) /* 假设“baz”是一个整数。 */ {:}

下划线很重要,因为 Fortran 使用该字符来保留
其符号直。如果您在 RS6000 上进行编译,则这不是
默认行为,但使用 -qextname 选项进行编译
RS6000 将使其执行与其他平台相同的操作。

从 Fortran 调用 C

请注意下面的 Fortran 示例以及它如何调用 C 例程:

调用 initscr() 调用clear() 。 。 。调用 move(x, y) 。 。 。称呼
刷新()调用endwin()结束

...其中 x 和 y 是指定新坐标的整数。

从 Fortran 调用 Curses

如果您调用“curses”例程“move()”,您可能会这样做
像这样的东西:

C 源文件包含“curses”的接口例程
库函数“move()”,以及其他 C 函数
为其他一些“curses”函数提供接口:

#include

initscr_() { initscr(); }

clear_() { 清除(); }

move_(x, y) int *x, y; / 这些是指针 */ { move(*x,
*y); }

刷新_() { 刷新(); }

endwin_() { endwin(); }

例程是使用以下命令编译的:

cc -ccurses.c f77 test.fcurses.o-lcurses-ltermcap

如果您正在使用 /usr/include/curses.h 中定义的宏
Fortran 文件,请注意它们采用 C 语言的约定
语言。请注意,这可能会影响您获得的结果
在 Fortran 中使用它们。


https://engineering.purdue.edu/ECN/Support/KB/Docs/CallingCFromFortran

Apparently FORTRAN 77 does not support dynamic memory allocation .
You could try allocating memory in a c program and passing the result back to the FORTRAN routine.

As done here
http://owen.sj.ca.us/~rk/howto/FandC/FandC.mem.html

Or more clearly - using some kind of an Interop to call a c method

from fortran.

The Unix portable Fortran-77 compiler ('f77') is written almost
entirely in C. The second pass of the compiler is the same one used by
the C compiler, and most 'f77' library routines are simply interfaces
to corresponding C library routines. However, since Fortran does not
support data structures like those used in C, you may not be able to
take advantage of all the functionality that the 'curses' library
offers. Manipulating windows with 'curses' is especially difficult. If
you are interested in using routines involving data structures, you
should probably use C instead of Fortran.

To call C routines from a Fortran program, you will have to write some
C code. Fortran passes arguments by reference or address, so the C
function has to be prepared to accept the variable as an address. This
means that you will have to write functions in C that are called from
Fortran that set up the arguments properly before calling the library
function. Schematically, this might be something like:

Note underscore ( _ ) in name of routine.

In the C source file:

foo_(bar) /* Note underscore */ int bar; / Note variables are
passed by address */

{ : }

In the Fortran source file:

call foo(baz) /* Assuming that "baz" is an integer. */ { : }

The underscore is important because Fortran uses the character to keep
its symbols straight. If you are compiling on an RS6000 this is not
default behavior, but compiling with the -qextname option on the
RS6000 will cause it to perform identical to the other platforms.

Calling C From Fortran

Note the Fortran example below and how it calls C routines:

call initscr() call clear() . . . call move(x, y) . . . call
refresh() call endwin() end

...where x and y are integers specifying the new coordinates.

Calling Curses from Fortran

If you were calling the 'curses' routine 'move()', you might do
something like this:

The C source file contains the interface routine to the 'curses'
library function 'move()', along with the other C functions that
provide an interface to the some other 'curses' functions:

#include

initscr_() { initscr(); }

clear_() { clear(); }

move_(x, y) int *x, y; / These are pointers */ { move(*x,
*y); }

refresh_() { refresh(); }

endwin_() { endwin(); }

The routines are compiled by using these commands:

cc -c curses.c f77 test.f curses.o -lcurses -ltermcap

If you are using macros defined in /usr/include/curses.h in your
Fortran file, be warned that they assume conventions of the C
language. Be aware that this may affect the results you obtain when
using them in Fortran.

from
https://engineering.purdue.edu/ECN/Support/KB/Docs/CallingCFromFortran

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文