: : : : :
: : : : : : Erg...sorry, I was too vague. Posting the real code would be too hard for me, since I'd have too much explaining to do. This mirrors my code enough. This was what my real question was about...
: : : : : :
: : : : : :
: : : : : : /*changing the const a, this is one way to do it*/
: : : : : : foo** changer;
: : : : : : changer=(foo**)&a;
: : : : : : *changer=&tmp;
: : : : : :
: : : : : :
: : : : : : 'a' is the argument/parameter to the function. It is defined as
: : : : : :
: : : : : : const foo* a
: : : : : :
: : : : : : tmp is the temporary variable containing the changed data.
: : : : : :
: : : : : : Now, to make the rest of the function use the my temporary variable when I use it, I changed the value of 'a' to the address of 'tmp'(the blue line), so that when this is executed...
: : : : : :
: : : : : : anotherFunc(a);
: : : : : : superFunc(a);
: : : : : : goFunc(a);
: : : : : : morefunc(a);
: : : : : :
: : : : : : ...the temporary variable is used instead of the user's. Also, if I need not change the inputted data, the original data is used.
: : : : : :
: : : : : : This is bad(I think), for it considered bad practice to change a const variable's value. So this is what my question is, whether I can still do this, though it is considered bad.
: : : : : :
: : : : : : I gave my reason for choosing to still code it this way despite that: I wanted to keep it const to make sure I dont alter the data that the pointer points to.
: : : : : :
: : : : : : Its exactly like this in my code. The const argument, the tmp, and changing the const variable.
: : : : : :
: : : : : : I hope this makes more sense...I hope. If not...may be I'll just go ahead and change the const's value.
: : : : : : {2}rIng
: : : : : :
: : : : :
: : : : : I don't quite understand. How exactly do you intend to change the constant variable? Shouldn't it yield an error. I don't think the compiler will allow you to change the value of the constant variable unless of course you remove the const property of the variable.
: : : : : As for the problem, i just thought you might want to try creating a constant pointer, although I have no idea if that will work in C!!
: : : : : Just a thought here. Haven't checked it out.
: : : : :
: : : :
: : : :
: : : : C is flexible/unsafe and will allow things that C++ wouldn't. Even though this is concidered as bad programming, this will work perfectly fine in C:
: : : :
: : : :
: : : : #include <stdio.h>
: : : :
: : : : int main()
: : : : {
: : : : const int x=0;
: : : : int* non_const_ptr = (void*)&x;
: : : : *non_const_ptr = 5;
: : : : printf("%d\n", x);
: : : : return 0;
: : : : }
: : : :
: : : :
: : : : It gave the result 5 on both gcc and Borland. However, whether the code works or not is system-specific. On a RAM based system like a PC, it will likely work without problems.
: : : :
: : :
: : : ...What kind of systems wouldn't it work on?...systems that aren't RAM based, like they use some other form of saving memory or something in which we can't just pop things onto an address (like virtual memory in which its not allowed...)
: : :
: : : {2}rIng
: : :
: :
: :
: : I'm actually surprised that it works on Windows, since it is supposed to do all kind of fancy virtual memory handling. But then, I don't know very much of what's going on behind the scenes in Windows.
: :
: : It wouldn't work on ROM-based systems, since constant variables will be placed in ROM and not RAM. Examples are microcontrollers, DSPs, systems with external memory etc.
: :
:
: Wait a sec, I forgot to mention something. The const pointer in my example(and in my code) really is a function argument/parameter...so it really ins't a const(unless the value passed is a const)...so it can't be in the ROM...
:
: int theFunc(const foo* a);
:
: since its a argument/parameter, it has to be variable...I guess on this one I can escape that...or do C compilers for these ROM-based systems treat the const keyword differently( ie. its only for real consts, and shouldn't be used as argument/parameter specifiers)
:
: Or do I understand wrong...
:
: {2}rIng
:
: P.S. which should i use: argument or parameter (in these posts)? I more comfortable with the former...but for others it doesn't
: seem the same...
:
const foo* a;
It means that you can't change the contents of 'a'. 'a' can point at data allocated in ROM or RAM, you haven't got a clue. I just wanted to point out a weakness in the language, I didn't say that you should use it.
If you write const before your pointer, the programmer who calls the routine gets comfortable, since he knows that the function won't change his precious data.
There is never any case where you want to change the contents or a const variable. If someone finds such a case, then it is because their program design is bad. It is fine to typecast from non-constant to constant, but never the other way around. Also, should you try it in C++, the compiler will give you a box on the ears.
Argument and parameter is the same thing. Parameter is a more commonly used term. None of them is more correct than the other.