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 1 3886
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. ...
|
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...
|
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...
|
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....
|
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...
| |
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;
|
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
|
by: d3x0xr |
last post by:
---- Section 1 ----
------
x.c
int main( void )
{
char **a;
char const *const *b;
b = a; // line(9)
|
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,...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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: 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...
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |