:: Re: [DNG] "Common knowledge?"-quest…
Etusivu
Poista viesti
Vastaa
Lähettäjä: Didier Kryn
Päiväys:  
Vastaanottaja: dng
Aihe: Re: [DNG] "Common knowledge?"-question
Le 22/01/2016 22:34, Rainer Weikusat a écrit :
> Can the effect of the following C function
>
> static void print_start(char const *name, char const *what)
> {
>      char *buf, *p;
>      unsigned name_len, what_len, total;

>
>      name_len = strlen(name);
>      what_len = strlen(what);
>      total = name_len + what_len + 3;

>
>      p = buf = alloca(total);
>      memcpy(p, name, name_len);
>      p += name_len;
>      *p++ = ' ';
>      memcpy(p, what, what_len);
>      p += what_len;
>      *p++ = ':';
>      *p = ' ';

>
>      *buf &= ~0x20;

>
>      Write(2, buf, total);
> }

>
> be considered obvious or should it rather get an explanation?
>
> An ASCII lowercase letter can be turned into the corresponding uppercase
> letter by clearing the sixth bit.


     toupper() wouild be better unless you can assert the first char is 
a lertter.


     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). The effect of this cannot be to protect the 
pointers in the caller program since they are passed by value -- copied 
into register or stack before passing control to the function. If it is 
for the sanity of the function itself, then congratulations, this is a 
sane thing to do, even if it is easy to check by eye that the pointers 
aren't modified. But, to be consistent, since your function doesn't even 
modify the content of the strings, you should also declare constant the 
characters themself, like in the following:
     static void print_start(const char const *name, const char const *what)


     Didier