M
Martin
I have a question regarding *use* of blocks.
In Plauger's THE STANDARD C LIBRARY in xfmtval.c in Chapter 6 I noticed in
function _Fmtval() that after some processing of 50 lines or so he creates a
block
char *_Fmtval( ... )
{
/* .. 50 lines or so of code ... */
{ /* build string in buf under control of fmt */
char *end, *s;
const char *g;
size_t i, ns;
for (s = buf; *fmt; ++fmt, s+= strlen(s))
/* ... */
}
return (buf);
} /* end function _Fmtval */
We all know this is legitimate, but I admit I have not seen this technique
employed before. Plauger also uses this technique in setlocal.c in the same
chapter.
Is this technique recommended, or are there caveats? I can see some C Coding
Guidelines excluding its use, if not specifically then by implication,
perhaps.
Having just added some code to an existing file in a void function, I
elected to employ this techique. It takes the form:
if ( error )
{
/* report error */
return;
}
if ( another_error )
{
/* report this error */
return;
}
/* ... a few more error checks ... */
/* no errors here, start processing */
{
unsigned char c;
/* ... more definitions ... */
/*... processing ... */
}
Which I think works well because the variables defined in the block are
necessary only if the errors looked for above are not present.
Has anyone any constructive comments about this technique?
In Plauger's THE STANDARD C LIBRARY in xfmtval.c in Chapter 6 I noticed in
function _Fmtval() that after some processing of 50 lines or so he creates a
block
char *_Fmtval( ... )
{
/* .. 50 lines or so of code ... */
{ /* build string in buf under control of fmt */
char *end, *s;
const char *g;
size_t i, ns;
for (s = buf; *fmt; ++fmt, s+= strlen(s))
/* ... */
}
return (buf);
} /* end function _Fmtval */
We all know this is legitimate, but I admit I have not seen this technique
employed before. Plauger also uses this technique in setlocal.c in the same
chapter.
Is this technique recommended, or are there caveats? I can see some C Coding
Guidelines excluding its use, if not specifically then by implication,
perhaps.
Having just added some code to an existing file in a void function, I
elected to employ this techique. It takes the form:
if ( error )
{
/* report error */
return;
}
if ( another_error )
{
/* report this error */
return;
}
/* ... a few more error checks ... */
/* no errors here, start processing */
{
unsigned char c;
/* ... more definitions ... */
/*... processing ... */
}
Which I think works well because the variables defined in the block are
necessary only if the errors looked for above are not present.
Has anyone any constructive comments about this technique?