:: Re: [DNG] "Common knowledge?"-quest…
Top Pagina
Delete this message
Reply to this message
Auteur: Didier Kryn
Datum:  
Aan: dng
Onderwerp: Re: [DNG] "Common knowledge?"-question
Le 23/01/2016 19:28, Rainer Weikusat a écrit :
> 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.
>
> [...]


     So, we could express it as the general declaration would be:


     <declaration statement>=<base type> <list of declarations>;


     <list of declaration> = <variable declaration>
                                        |  <list of declarations>, 
<variable declaration>


     <variable declaration>=<level of indirection and qualification> 
<variable name>


     <level of indirection and qualification> = <level of indirection> 
<qualifier>

| <level of indirection and qualification> <level of indirection>

<qualifier>

     To simplify the description, I assume <qualifier> can be null.


     For example:


     char x,  * const *p;
     where p would be a pointer to a constant pointer to a char.


     But another syntax is allowed, where the qualifier[s] can be on the 
left. I'm not sure there's a BNF description of the C syntax.


>> 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.


     The most important goal is not to optimise the executable, it is to 
protect against programmer's errors. gcc will warn you if you try to 
modify a constant.


     Didier