This is what I want to do:
1) I want a function that instantiates a data structure.
void instantiateCDB(void);
2) I also want a function that updates the data structure that is instantiated and returns a const pointer to the data structure (to make it read-only)
I know that this can be done in C++/Java. But can it also be done in C?
The program flow that I want to write is:
main(){
instantiateCDB(); // Allocates a CDB
const struct canDataBlock * cdb = getUpdateSystem();
}
// But the best function definitions that I can come up with is this.
struct canDataBlock * instantiateCDB() {
static struct canDataBlock cdb = {0};
return &cdb;
}
const struct canDataBlock * getUpdateSystem() {
struct canDataBlock * cdb = instantiateCDB();
return &cdb;
}
The problem is: How do I access the data structure with write/read access instantiated in the instantiateCDB
function if it would be declared void? If I am going to return the allocated data structure, the user can alter the canDataBlock
thus losing its integrity. What I want to happen is only the getUpdateSystem()
can change the values of the data structure instantiated by the instantiateCDB()
function. How do I solve this problem? Is there another technique in C that I do not know about. If there is, please teach me. :)