将 C 字符串和 fortran 字符串混合在一个文件中
我有一个关于在一个文件中混合 C 字符串和 fortran 字符串的问题。
假设我正在使用固定长度 9 的名称字符串,我定义了一个长度宏,就像
#define NAME_LEN 9
在 .c 文件中一样。
有一个现有的 fortran 函数,让我们将其命名为 fortran_function(char* name)
现在我必须在 ac 函数中调用这个 fortran 函数,我们的名称是
c_function(char name[]) {
fortran_function(name)
}
现在的问题是,我应该如何声明 c_function签名?
c_function(char name[])
c_function(char name[NAME_LEN +1])
或者
c_function(char name[NAME_LEN])
在什么情况下,我应该使用 9 作为名称长度或 10?
我的理解是,只要将一个包含 9 个字符的空终止字符串传递给 c_function,所有声明都是正确的。是这样吗?
还有其他问题应该放在这里吗?有潜在的错误吗?
I have one question with mixing C-string and fortran-string in one file.
Supposed I am playing with the name string with fixed length 9, I define a length Macro like
#define NAME_LEN 9
in a .c file.
There is an existing fortran-function, let's name it fortran_function(char* name)
Now I have to call this fortran function in a c function, let's name is
c_function(char name[]) {
fortran_function(name)
}
Now the problem is, how should I declare the c_function signature?
c_function(char name[])
c_function(char name[NAME_LEN +1])
or
c_function(char name[NAME_LEN])
Under what situations, I should use 9 as name length or 10?
My understanding is that, as long as you passed a null-terminated string with 9 characters to the c_function, all the declaration are correct. Is that right?
Any other concern should be put here? Any potential bugs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我没记错的话,这里还有一个问题。 Fortran 不使用以 null 结尾的字符串;相反,它用 0x20(空格)填充缓冲区的右端。因此,如果您可以访问 Fortran 源代码,我将修改函数签名以将传入字符串的长度作为参数。否则,您可能会使代码的 Fortran 端崩溃。
There's one more gotcha here, if I remember correctly. Fortran does not use null-terminated strings; instead, it pads the right end of the buffer with 0x20 (space). So, if you have access to the Fortran source, I would modify the function signature to take the length of the passed-in string as an argument. Otherwise, you will probably crash the Fortran side of the code.
Dave 是正确的,字符串的标准 Fortran 概念是固定长度,在右侧用空格填充。 (Fortran 现在也有可变长度字符串,但这些还不常见,并且与 C 互操作会非常棘手。)如果您希望固定长度,则在 Fortran 代码中使用相同的参数 NAME_LEN ,并具有相同的值。戴夫关于额外长度论证的建议可能更好。
另一个改进是在 Fortran 端使用 ISO C 绑定工具(根据注释进行更正!)
等。“绑定”名称是 C 可以调用 Fortran 例程的名称 - 它可以与 Fortran 名称不同。作为 iso_c_binding 的一部分,还提供了符号 C_NULL_CHAR,您可以在 Fortran 代码中使用它来提供 C 期望的终止空字符等。
Dave is correct, the standard Fortran concept for strings is fixed-length, padded with blanks on the right. (Fortran now also have variable length strings, but these are not yet common and would be very tricky to inter-operate with C.) If you want the lengths fixed, then have the same parameter NAME_LEN in your Fortran code, with the same value. Dave's suggestion of an additional length argument is probably better.
An additional refinement is to use the ISO C Binding facility on the Fortran side (corrected per the comment!)
etc. The "bind" name is the name by which C can call the Fortran routine -- it can be different from the Fortran name. Also provided as part of the iso_c_binding is the symbol C_NULL_CHAR which you can use in the Fortran code to provide the terminating null character that C expects, etc.
这些电话没有区别。 C 编译器会将它们全部视为 char*。您只需确保在 c 中使用它之前将其 null 终止即可。如果您仅在 fortran 端使用该字符串并且 c 函数只是保留它,那么您不需要执行任何操作。
There's no difference to those calls. The c compiler will treat them all as char*. You just have to make sure you null terminate it before you use it in c. If you're only using the string in the fortran side and the c functions are just holding on to it, then you don't need to do anything.