473,624 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const char *const

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$EoUw5jJ PPvAPECNaaWzMK/";

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
1 3906
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$EoUw5jJ PPvAPECNaaWzMK/";

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("Selecte d: %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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2228
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at compile time. The same source code compile if I don't try to split it up into separate libraries. ...
5
2152
by: TechCrazy | last post by:
What do each of these mean? Thanks. I am incredibly confused. char foo (const char * &p ); char foo (const char &* p ); char foo (const &char * p ); char foo (const char * const &p ); char foo (const char * &const p ); char foo (const char &* const p ); char foo (const &char * const p );
7
4352
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 type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
8
2582
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g. char * -> const char *. However, this does not appear to work when I add another level of indirection: void test1 (char **value) {}
6
10020
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he hoped I could solve, and I couldn't. Some code he's using, written in 1999, that compiled fine in 1999, no longer does in 2006 with g++ 4. This little bit of code: SimS::SimS (ostream &s) {
10
5309
by: dwaach | last post by:
Hi, I am trying to compile the following program, #include <iostream> using namespace std; typedef char* CHAR; typedef const CHAR CCHAR;
42
32143
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
10
2774
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1867
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, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
9
10517
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int strspcmp(const char * s1, const char * s2) {
0
8242
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8629
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7170
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2611
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
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.