Re: const qualifier - in declaration, definition or both?



Pietro Cerutti wrote, On 31/08/07 13:57:
Asbjørn Sæbø wrote:
Suppose I have a function that takes a pointer as its input, but does
not change what the pointer points to. In that case, the const
qualifier can be used to indicate that the variable pointed to is not
changed. But should this const qualifier be used in the function
declaration, the function definition, or both?

If you don't use it in both you may get warnings or even an error
(depending on the compiler settings):


void nop(const int *i);
void nop(int *i)
{
(void)i;
}

<snip>

The compiler is required to issue a diagnostic (commonly a warning or error) so there is not really a may about it.
--
Flash Gordon
.



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... and make all strings const save where the intent ... function whose contract is to change the string. ... the compiler "just" prevents the string ... try to do using the pointer you get. ...
    (comp.lang.c)
  • Re: Is this legal ?
    ... >> to be followed again if the compiler can't guarantee that nothing that ... >address of an object into a pointer to const type version, ... to change it (by casting away const from a pointer to it, ...
    (comp.lang.c)
  • Re: Bug found in GCC-AVR/ AVRStudio
    ... >>assembly code for reading flash and reading ram are quite different, ... >>const data in flash. ... >You can always assign a pointer to a more qualified (with const and/or ... I am pretty sure ImageCraft's AVR compiler does what Jack is saying. ...
    (comp.arch.embedded)
  • Re: MSDN const_cast sample
    ... modified was originally created as const. ... I see no reason to believe that the compiler won't use the register used to ... Exceptional C++ Item 43 (const-correctness) pg. ... a reference or pointer to const-qualified type that actually ...
    (microsoft.public.vc.language)
  • Re: MSDN const_cast sample
    ... modified was originally created as const. ... I see no reason to believe that the compiler won't use the register used to ... Exceptional C++ Item 43 (const-correctness) pg. ... a reference or pointer to const-qualified type that actually ...
    (microsoft.public.vc.language)