:: Re: [DNG] "Common knowledge?"-quest…
Page principale
Supprimer ce message
Répondre à ce message
Auteur: Didier Kryn
Date:  
À: dng
Sujet: Re: [DNG] "Common knowledge?"-question
Le 24/01/2016 19:14, KatolaZ a écrit :
>> Soo, the above is nearly the same as
>> >
>> > char buf[total];
>> > p = buf;
>> >
>> >Why then use alloca()?
>> >
> Maybe because
>
>    char buf[total];

>
> works only in ANSI C11. I still don't see the need for an internal
> buffer to print out a formatted string, to be honest:)


     The following works in plain old C:


#include <stdio.h>
#include <string.h>
static void print_start(char const *name, char const *what)
{
      unsigned name_len, what_len, total;


      name_len = strlen(name);
      what_len = strlen(what);
      total = name_len + what_len + 3;
      {
        char buf[total], *p=buf;
        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);
      }
}


     Embedded subprograms have other use cases. In long programs, they 
allow to declare variables with a limited scope, just near where they 
are used.


     Didier