423,850 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

const char *const

P: n/a
Hi, can somebody explain the following syntax to me.
This is straight from a gnu info file:

int
main(void)
{
/* Hashed form of "GNU libc manual". */
const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/";

I think the idea at play here is whether the pointer is constant or not ?
My guess is this:
const char * is a pointer to a "constant char" type ?
if you want the pointer itself to be constant too, you make that
const char *const ... ?

so the "*const" means the pointer itself is constant.

so,
int *const would be a "constant pointer" to an int, which itself may be changed however ?

I supposed I will play w/ the compiler to see if this is the right interpretation,
after typing this much though, i'm going to go ahead and send this off
%^)

e
Nov 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sat, 21 Feb 2004 02:28:01 GMT, electric sheep <el******@null.invalid>
wrote:
Hi, can somebody explain the following syntax to me.
This is straight from a gnu info file:

int
main(void)
{
/* Hashed form of "GNU libc manual". */
const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/";

I think the idea at play here is whether the pointer is constant or not ?
My guess is this:
const char * is a pointer to a "constant char" type ?
if you want the pointer itself to be constant too, you make that
const char *const ... ?

so the "*const" means the pointer itself is constant.

so,
int *const would be a "constant pointer" to an int, which itself may be changed however ?
Right; you can change the int value by using the pointer, e.g.:

int i;
int *const pi = &i;
++*pi; // OK

Looks like you explained the syntax to yourself pretty well.

You'll find that pointer-to-const is much more prevalent than
const-pointer; Some folks prefer to define local variables that never need
to change to be const as a sort of pre-emptive strike against accidental
change of those variables, but whether or not that is good style seems to
be a Religious Issue. Personally, seeing a function definition like this:
void foo(const int x) { ... }
seems really weird to me (whether x is an int /or/ a pointer).

Here's a scenario where const pointer to const might be useful, though: We
have an array of pointers to constant C strings, and wish to call a
function that selects one of them randomly:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

//
// Return one of the pointers in the array randomly, or "none" if
// size is zero:
//

const char *select(const char * const list[], size_t size)
{
return (size == 0) ? "none" : list[rand() % size];
}

int main()
{
const char *const names[] = {"me", "him", "someone else"};

srand(time(0));
printf("Selected: %s\n", select(names, 3));
return EXIT_SUCCESS;
}

The select() function as written will work with an array of non-const
pointers, too, of course. But in order to work with appropriately defined
data in, say, ROM, the 'const' after the '*' is necessary. Without it:

const char *select(const char * list[], size_t size) { ... }

using 'names' as defined in main() above would violate constness (should
draw at least a warning.)

Interestingly, in the select() function, the 'list' parameter can /also/ be
declared const, independently of the other two consts; i.e., the
declaration

const char *select(const char * const * const list, size_t size);

says that in addition to the other two things being const, so is the
pointer that the array name (in this case, 'names' from main) decays to in
the call. Whether this is good practice is the "religious issue", but the
other two consts here are more cut-and-dried.
-leor

I supposed I will play w/ the compiler to see if this is the right interpretation,
after typing this much though, i'm going to go ahead and send this off
%^)

e


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.