[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CConst
In functions const can be used to indicate a 'read-only' from ?struct
To indicate that MyStruct is only read from, use transmogrify1 and transmogrify3. To save typing, many choose transmogrify1.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CConst
(C) const
Keyword being an abbreviation of 'constant'. Use const whenever possible.
int main()
{
const int x = 69;
return 0;
}
In functions const can be used to indicate a 'read-only' from ?struct
typedef struct
{
int mX;
} MyStruct;
/* Most common */
void transmogrify1(const MyStruct * pMyStruct)
{
/* pMyStruct->mX = 12; Error: cannot modify where pMyStruct points to */
pMyStruct = 0; /* Okay, won't do harm to pMyStruct */
}
/* Most uncommon */
void transmogrify2(MyStruct * const pMyStruct)
{
pMyStruct->mX = 12; /* Okay, can modify where pMyStruct points to */
/* pMyStruct = 0; Error: cannot modify where pMyStruct points to */
}
/* Common */
void transmogrify3(const MyStruct * const pMyStruct)
{
/* pMyStruct->mX = 12; Error: cannot modify where pMyStruct points to */
/* pMyStruct = 0; Error: cannot modify where pMyStruct points to */
}
To indicate that MyStruct is only read from, use transmogrify1 and transmogrify3. To save typing, many choose transmogrify1.
'const' links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
