【问题标题】:Shared global const variables in C, defined in mexFunction()C 中的共享全局 const 变量,在 mexFunction() 中定义
【发布时间】:2014-06-16 20:02:25
【问题描述】:

我想拥有 const 的全局变量,但它们应该在 mexFunction() 函数中定义。这是因为它们应该设置为一些来自 Matlab 的输入值。 (mexFunction() 基本上是我的 main() 函数。)

这样的事情可能吗?

main.h

extern int const myConstGlobal;

main.c

mexFunction(input)
{
    int const myConstGlobal = input;
}

函数.c

#include main.h

foo(myConstGlobal){}

我目前理解的一些链接:

shared-global-variables-in-C 中解释了如何使用共享全局变量。

the second answer of this link 中解释了如何使用共享的全局 const 变量

...你必须声明:

extern int const const_int ;

在标题中,并且:

extern int const const_int = fn() ;

在一个(并且只有一个)源文件中。

但是像这样我需要函数来传递我想要规避的输入值。

【问题讨论】:

  • 你必须把它的定义放在一个函数之外。否则它不是全局的。

标签: c constants declaration definition mex


【解决方案1】:

JaBe - 我对 Const 的理解是它不能改变,它是一个固定的设定值。

话虽如此,这是c对吗?您应该能够获取 const 的地址并将其放在指针中。

int *myConstGlobalPointer = &myConstGlobal;

如果编译器允许你这样做,你可以设置 myConstGlobalPointer = input; 这应该设置 myConstGlobal 的值。

诀窍是不要将 const 指针称为 const 指针,这可能会起作用。

提姆

【讨论】:

    猜你喜欢
    • 2022-07-14
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多