0

I want to have global variables which are const, but they should be defined in the mexFunction() function. This is because they should be set to some input values, which come from Matlab. (The mexFunction() is basically my main() function.)

Is something like this even possible?

main.h

extern int const myConstGlobal;

main.c

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

functions.c

#include main.h

foo(myConstGlobal){}

Some links from which I have my current understanding:

How to work with shared global variables is explained in shared-global-variables-in-C.

How to work with shared global const variables is explained in the second answer of this link

...you have to declare:

extern int const const_int ;

in the header, and:

extern int const const_int = fn() ;

in one (and only one) source file.

But like that I need functions to pass the input values which I want to circumvent.

Community
  • 1
  • 1
JaBe
  • 664
  • 1
  • 8
  • 27
  • You have to put the definition of it outside of a function. Otherwise it is not a global. – M.M Apr 30 '14 at 14:11

1 Answers1

0

JaBe - my understanding of Const is that it cannot be changed, it is a constant set value.

That being said, this is c right? you should be able to get the address of the const and place it in a pointer.

int *myConstGlobalPointer = &myConstGlobal;

if the compiler is forgiving, and lets you do this you can set myConstGlobalPointer = input; This should set the value of myConstGlobal.

The trick is not calling the const pointer a const pointer, this just might work.

Tim