473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

const qualifier - in declaration, definition or both?


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ø
Aug 31 '07 #1
4 1898
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;
}

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)
With kind regards
Asbjørn Sæbø

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 31 '07 #2
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
Aug 31 '07 #3
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?

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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Aug 31 '07 #4
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Pietro Cerutti wrote, On 31/08/07 13:57:
Asbjørn Sæbø wrote:
[...]
[S]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
Sep 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
3
by: Philippe Mesmeur | last post by:
J'ai eu une longue discussion hier au sujet des parametres de fonction const. La personne etait pour mettre "const" devant TOUS les parametres ne devant pas etre modifies. A mon avis, il faut...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
7
by: Martin | last post by:
I've searched this newsgroup's archives but couldn't find any posts that provided any "rule of thumb" recommendations for use of const in function parameters. I appreciate there is no need for...
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: Ben Petering | last post by:
Hi group, this is a 'best practice' type question (I want discussion of the issue - whys and why nots - similar to "casting the return value of malloc()", to cite an analogous case). Let's...
7
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.