473,396 Members | 1,713 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,396 software developers and data experts.

order of const in function prototypes

Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)

Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.

moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)
function4( int* const a )

Thanks in advance for any quidance here.
Jul 22 '05 #1
7 3775
johny smith wrote:
Can someone please explain to me the difference between these two:

int function1(const int a);
int function2(int const a);

Both seemed to compile
but what is the difference between the two above. cat main.cc int function(const int a);
int function(int const a);

int function(int a) {
return a;
}

int main(int argc, char* argv[]) {
return function(13);
}
g++ -Wall -ansi -pedantic -o main main.cc
There is no difference between any pair of declarations

int function(int a);
int function(const int a);

and

int function(int const a);
And why would one choose one over the other. moreover, I thought there was a difference between the two below where one
would not let the value change whereas

the other one would not let the address of the pointer change,
but I must be missing something here.

int function3(const int* p);
This declaration is equivalent to

int function3(int const* p);

This means that function3(const int*) cannot change *p.
int function4(int* const p);


This *declaration* is equivalent to

int function4(int* p);

The difference is in the function *definition*

int function4(int* const p) {
*p = 13; // OK! *p is mutable
// . . .
++p; // error! p is const
return *p;
}
Jul 22 '05 #2
johny smith wrote:

Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)

Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.

"const int a" and "int const a" are technically the same. In general, the
former pattern is more popular, the latter has arguably a more consistent
behaviour (some may disagree). Here is an illustration of what I mean:
Assume expression EXPR represents some type. Then EXPR const (where it is
allowed) will always have the same meaning: constant EXPR. The meaning of
const EXPR, however, will depend on what EXPR is:

typedef any_type_name EXPR;
//fine: const EXPR is the same as EXPR const

#define EXPR type_name*
//const EXPR or, directly, const type_name* now means something
different: a pointer-to-const data, not a const pointer-to-data.

By the way, a const modifier on the type of a function parameter
doesn't do anything except for preventing the function from
modifying its /own copy/ of the argument. It doesn't make sense
to use it here.
moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)
a is a pointer to constant data (it would not let you change the pointed-to
value); everything is fine.
function4( int* const a )

a is a constant pointer to data; it would not let you change the /value/
of the pointer a (the address of a variable never changes). This const is
rather, sorry, pointless, as in the case of function1 and function2.

Denis
Jul 22 '05 #3
On Tue, 29 Jun 2004 20:15:51 -0600, "johny smith"
<pr**************@charter.net> wrote in comp.lang.c++:
Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)
No difference at all here. According to the grammar, the 'const'
keyword modifies what is immediately to its left (before it). An
exception is made to allow it to be first.
Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.

moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)
function4( int* const a )

Thanks in advance for any quidance here.


What you thought was exactly correct:

const int *a; // a is a modifiable pointer to constant int
int const *a; // a is a modifiable pointer to constant int
int * const a; // a is a constant pointer to modifiable int
const int *const a; // a is a constant pointer to a constant int
int const *const a; // a is a constant pointer to a constant int

So what makes you think you are missing something?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html>

Jul 22 '05 #4

"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)

Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.

moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)
function4( int* const a )

Thanks in advance for any quidance here.


What matters is whether const goes before or after *, whether it goes before
or after int is immaterial.

function3a( const int* a)

function3b( int const* a)

function4( int* const a)

3a and 3b are the same, 4 is different.

john
Jul 22 '05 #5
"johny smith" <pr**************@charter.net> wrote:
Can someone please explain to me the difference between these two:

function1( const int a)
function2( int const a)
First of all, 'const' applies to all variable declarations, not just to
the declaration of function parameters. In fact, the above use of 'const'
may be confusing because it does not change the signature of the function
but only applies to the use of the variables within the function. That is,
the 'const' only applies to the use of the parameter within the function,
and the function can be declared with or without the 'const' and then
defined with or without the 'const'.

Concerning your original question: There is no difference between these
two uses of 'const'. In both cases the object 'a' is made constant.
Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.
My strong preference is to place the 'const' to the right because it is
consistent with other uses of 'const' where you don't have the choice:

- 'int* const', ie. a const pointer to a non-const 'int'; you cannot place
the 'const' differently to achieve the same effect.
- 'struct S { void f() const; };', ie. a const member function which actually
just means that the object pointed to by 'this' is 'const', ie. is
implicitly declared as 'S const* const this'; you cannot place the 'const'
differently.

In addition, it is pretty simple to decode what is made 'const': the type
immediately to the left of the 'const', e.g.:

- 'int const*' is a non-const pointer to a const 'int' (the pointer can be
changed but not the pointed to object)
- 'int* const' is a const poniter to a non-const 'int' (the pointer cannot
be changed but the pointer to object can)
- 'int const* const' is a const poniter to a const 'int' (neither the pointer
nor the object can be changed)

Another argument for my preference of the 'const' placement is soemthing
I stumbled over quite a while. Initially, I had code like this (the involved
types where something else than 'int' but this does not matter):

void f(const int*);

.... and a typedef was introduced:

typedef int* int_ptr;

.... and I modified the declaration to use this typedef:

void f(const int_ptr);

to use this typedef. Of course, I changed the semantics! The original
function took a pointer to a const 'int' as parameter. After the replacement
the function took a const pointer to a non-const 'int' as parameter. It
would have been obvious that this replacement won't work if the declaration
had been

void f(int const*);

in the first place.

The two arguments in favour of the other placement of 'const' are that C
uses this placement (but C has a much less elaborated notion of constness)
and that most people use it. To me, neither of these arguments are sufficient
to abandon the more readable and consistent notation.
moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.

function3( const int* a)
function4( int* const a )


The former lets the pointer change but not the pointee, the latter lets
the pointee change but not the pointer. It would have been more obvious
if the first of these declarations had used the more readable

function3( int const* a )

Also, the 'const' in the second declaration only applies to the definition
of the function.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #6
E. Robert Tisdale posted:

There is no difference between any pair of declarations

int function(int a);
int function(const int a);

and

int function(int const a);

Yes there is.

int function(int a)
{
return ++a;
}
No problem.
int function(const int a)
{
return ++a;
}

Problem.
int function(int const a)
{
return ++a;
}

Problem.
-JKop
Jul 22 '05 #7

"JKop" <NU**@NULL.NULL> wrote in message
news:lH*****************@news.indigo.ie...
E. Robert Tisdale posted:

There is no difference between any pair of declarations

int function(int a);
int function(const int a);

and

int function(int const a);

Yes there is.

int function(int a)
{
return ++a;
}
No problem.
int function(const int a)
{
return ++a;
}

Problem.
int function(int const a)
{
return ++a;
}

Problem.


Much as I hate to agree with Bob, he said 'no difference between the
declarations', you posted definitions. As prototypes these are all
identical.

int function(int a);
int function(const int a);
int function(int const a);

It's yet another obscure corner of C++, read first para of Dietmar's post.

john
Jul 22 '05 #8

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

Similar topics

11
by: John D. Goulden | last post by:
Hopefully this question will make sense; if not, please correct my thinking :) If I wish to create a one-dimensional array at run time, I write int * myArray = new int ; and pass that array...
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...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
7
by: questions? | last post by:
I have a file including all my function prototypes. I include them at the most top of my program. But some of the prototypes include the structures I declared later. I didn't notice the problem...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
13
by: James Brown | last post by:
This may seem a little odd but I have a few simple questions. Basically, I'm writing a really simple parser which understands C type declarations - the only reason being, so that I understand C...
7
by: James | last post by:
Hello, First off, I know this code will not compile and am not asking for someone to solve it for me. What I am asking is from the code below, how does one first define an array as a...
29
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment:...
9
by: istillshine | last post by:
It seems I never need to use it. It only made a long line longer, and made you type five more characters. I feel so strange why people are talking about it.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.