const qualifier - in declaration, definition or both?

G

Guest

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?

Example:

In .h-file
int foo(const int *bar);

In .c-file
int foor(const int *bar)
{
return *bar + 2;
}


With kind regards
Asbjørn Sæbø
 
P

Pietro Cerutti

Asbjørn Sæbø said:
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;
}

int main(void) {
int i = 12;
nop(&i);
return (0);
}

ctemp.c:4: error: conflicting types for 'nop'
ctemp.c:1: error: previous declaration of 'nop' was here

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

int main(void) {
int i = 12;
nop(&i);
return (0);
}

ctemp.c:4: error: conflicting types for 'nop'
ctemp.c:1: error: previous declaration of 'nop' was here

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

int main(void) {
int i = 12;
nop(&i);
return (0);
}

(builds clean)
 
C

CBFalconer

Asbjørn Sæbø said:
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?

Example:

In .h-file
int foo(const int *bar);

In .c-file
int foor(const int *bar)
^^^^
Assuming that should read 'foo'
{
return *bar + 2;
}

That's just fine. The return value is delivered, and anything done
to it will not disturb the original foo.
 
F

Flash Gordon

Pietro Cerutti wrote, On 31/08/07 13:57:
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.
 
G

Guest

Flash Gordon said:
Pietro Cerutti wrote, On 31/08/07 13:57:
Asbjørn Sæbø said:
[...]
hould 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):


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


OK. Thanks to both of you!

Asbjørn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top