controlling modifying global variable

This is a discussion on controlling modifying global variable within the C Programming forums, part of the General Programming Boards category; Hi.
I am coding in C.
i have a global variable.
I want only one function to change its value, ...

If it's a struct, you might be better off passing it as a const pointer foe efficiency's sake. Const means they can't change it.
You could make the functions accept a const argument anyway to catch any errors trying to modify the variable when they shouldn't.

Use a local variable instead, and only pass it by value to all other functions except that one function, where you pass its address instead of its value.

Of course, it doesn't have to be a local variable, just as long as you pass it to code that doesn't have the right to access it directly, and either use pass by value or by const pointer (for larger pieces of data pass by pointer is more efficient, and const will ensure that your code doesn't accidentally write to the data).

Of course, if you make it global, there's still the chance that some code will access the variable AS A GLOBAL, which can only be detected by code inspection [or randomly changing the name of the variable and the allowed changes and see what breaks!]

If it's a struct, you might be better off passing it as a const pointer foe efficiency's sake. Const means they can't change it.

Ah yes, I had built-in types in mind. That said, I would clarify it as having a pointer to const parameter, or a pointer to const that is itself const, since const pointer usually means pointer that is const instead of pointer to const.

Of course, it doesn't have to be a local variable, just as long as you pass it to code that doesn't have the right to access it directly

That seems like a non-solution though since it disallows by convention without enforcing the convention in code.

Ah yes, I had built-in types in mind. That said, I would clarify it as having a pointer to const parameter, or a pointer to const that is itself const, since const pointer usually means pointer that is const instead of pointer to const.

Of course, you're right. I'm always think about how it's written like "const type*" (could be read like const pointer) rather than "type* const" (could be read like pointer to const). Just a little confusing

You could also hide a global variable inside a source file and use access functions. One for read, one for write. Functions can call appropriate functions to get a pointer to the global variable (const for read).

You could also hide a global variable inside a source file and use access functions. One for read, one for write. Functions can call appropriate functions to get a pointer to the global variable (const for read).

Yes, and to avoid accidental use elsewhere, make it a static global variable, so only those functions in that file can actually see the variable.