473,563 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of const in parameter specification

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 scalar variables to have const defined,
but how far should it go with pointers?

The Standard Library has functions defined that specify const, for example,
puts(const char *s) but why not go the extra bit and make it puts(const char
* const s)?

Any recommendations with rationale gratefully received.

--
Martin
http://martinobrien.co.uk/

Nov 14 '05 #1
7 1701
"Martin" <martin.o_brien @[no-spam]which.net> writes:
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 scalar variables to have const defined,
but how far should it go with pointers?

The Standard Library has functions defined that specify const, for example,
puts(const char *s) but why not go the extra bit and make it puts(const char
* const s)?

Any recommendations with rationale gratefully received.


`const char *s' means that the memory pointed to by `s' cannot be
modified through `s'. In the context of a function prototype, you can
consider this a promise: The function `puts' promises not to modify
the string you pass as argument.

`char *const s' means that the pointer itself cannot be modified. But
remember that `puts' has its own copy of the pointer. The caller doesn't
need to know if `puts' modifies its copy of the pointer, since that
wouldn't have any influence on the caller anyway.

For this reason, const (and volatile, and [in C99] restrict) qualifiers
on the parameter itself (but not on what the parameter points to, if it
is a pointer) can always be omitted in prototype declarations. It is
valid to declare, e.g., the following prototype

void foo (int a, int *b, const int *const *c);

and then define the function as

void foo (const int a, int *const b, const int *const *const c)
{
/* ... */
}

Martin
Nov 14 '05 #2
Martin Dickopp <ex************ ****@zero-based.org> writes:
`char *const s' means that the pointer itself cannot be modified. But
remember that `puts' has its own copy of the pointer. The caller doesn't
need to know if `puts' modifies its copy of the pointer, [...]

^^^^^^^^
My statement was a bit ambiguous. In case it's not clear from the
context: The underlined text refers to the copy of the pointer in the
`puts' function, /not/ the copy in the caller.

Martin
Nov 14 '05 #3
On Sun, 15 Feb 2004 23:25:14 -0000, in comp.lang.c , "Martin"
<martin.o_brien @[no-spam]which.net> wrote:
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 scalar variables to have const defined,
but how far should it go with pointers?
if a function doesn't modify the contents of a pointer argument, then make
it const to a) remind the function maintainer not to try and b) inform the
caller that its safe to assume its unchanged.
The Standard Library has functions defined that specify const, for example,
puts(const char *s) but why not go the extra bit and make it puts(const char
* const s)?


That says that the pointer itself is unchangeable. There's no benefit in
that since the fn gets a copy of the pointer anyway, and changing the copy
won't have any effect on the caller.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #4
In <cu************ *@zero-based.org> Martin Dickopp <ex************ ****@zero-based.org> writes:
"Martin" <martin.o_brien @[no-spam]which.net> writes:
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 scalar variables to have const defined,
but how far should it go with pointers?

The Standard Library has functions defined that specify const, for example,
puts(const char *s) but why not go the extra bit and make it puts(const char
* const s)?

Any recommendations with rationale gratefully received.
`const char *s' means that the memory pointed to by `s' cannot be
modified through `s'.


Of course it can:

*(char *)s = 'a';

It need not even invoke undefined behaviour, depending on where s actually
points.
In the context of a function prototype, you can
consider this a promise: The function `puts' promises not to modify
the string you pass as argument.


A "promise" only to the human reader: the compiler still cannot rely on it
when optimising, for the above shown reason.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
Mark McIntyre wrote:
That says that the pointer itself is unchangeable. There's
no benefit in that since the fn gets a copy of the pointer
anyway, and changing the copy won't have any effect on
the caller.


I suppose the only benefit is to trap any inadvertent attempts to
change that local pointer, indicating that the author is attempting
something he or she shouldn't.

Martin
http://martinobrien.co.uk/
Nov 14 '05 #6
ma************@ which.net (Martin O'Brien) writes:
Mark McIntyre wrote:
That says that the pointer itself is unchangeable. There's no benefit
in that since the fn gets a copy of the pointer anyway, and changing
the copy won't have any effect on the caller.


I suppose the only benefit is to trap any inadvertent attempts to
change that local pointer, indicating that the author is attempting
something he or she shouldn't.


That's not achieved by making the pointer const in the prototype
declaration, unless the declaration is also the definiton. Only
the qualifier in the definition counts, while the qualifier in the
(non-definition) prototype declaration will be ingored.
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #7
"Martin Dickopp" wrote:
That's not achieved by making the pointer const in the prototype
declaration, unless the declaration is also the definiton. Only
the qualifier in the definition counts, while the qualifier in the
(non-definition) prototype declaration will be ingored.


Thanks for that Martin.

I'm only talking about the function definition.

--
Martin
http://martinobrien.co.uk/

Nov 14 '05 #8

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

Similar topics

12
3405
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using static const * int class members as opposed to using enumerations to accomplish a * similar goal. The use of static constants seems more explicit and...
16
12260
by: Peter Ammon | last post by:
Often times, I'll have some malloc()'d data in a struct that need not change throughout the lifetime of the instance of the struct. Therefore, the field within the struct is declared a pointer to const . But then free() complains when I pass in the pointer because free() is declared as void free(void*). So I have to cast. Why is it not...
6
8199
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the generic pointer type. My real question is, is (void *) such a generic pointer type that it
24
2302
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other compilers like GCC though. Many thanks! - Kevin
16
3145
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed...
6
8292
by: Spoon | last post by:
Hello, I don't understand why gcc barks at me in this situation: $ cat foo.c extern void func(const int * const list, int nent); int main(void) { int *p;
9
1786
by: Ben Voigt | last post by:
Found another variation on my previous bug (https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100917) I would welcome validation and votes. class ISkewEstimator {
2
9149
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
3
3091
by: Chameleon | last post by:
The following code produces strange errors in mingw. Is a C++ problem or compiler problem? ---------------------------- #include <list> class A { static const int B = 0; std::list<intlst; void calc();
0
7659
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.