:: Re: [DNG] "Common knowledge?"-quest…
Kezdőlap
Delete this message
Reply to this message
Szerző: Rainer Weikusat
Dátum:  
Címzett: dng
Tárgy: Re: [DNG] "Common knowledge?"-question
Didier Kryn <kryn@???> writes:
> Le 23/01/2016 12:16, Didier Kryn a écrit :
>>
>>     I'm curious of the reason why you specify
>>    static void print_start(char const *name, char const *what)

>>
>>     This means the pointers to the arrays of characters are constant
>>     (not the characters).


[...]

>     Sorry, I overlooked the code and read "char const *name" as if it
> was "char * const name". Actually the syntax you used is equivalent to
> "const char *name".


That's a habit based on an observation I made when I started to write
code in C: Type qualifiers are left-associative (presumably a misuse of
the term) unless there's nothing to the left of them, eg

const char *s;

declares a pointer to a constant string,

char const *s;

does the same and

char * const s;

declares a constant pointer to a non-constant string. If the qualifier
is aways to the right of the thing it's supposed to qualify, it
uniformly binds to what's left of it.

[...]

> It actually protects the string from being overwritten by the
> function.


Not really. It just asserts that the string won't be modified using this
pointer. In theory, this would enable a compiler to optimize such
accesses, eg, collapse many of them into one, however, in practice, gcc
doesn't do that, and the qualifier is useless.