This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Function definition within function
- From: renato dot astorino at hotmail dot com
- To: gcc at gcc dot gnu dot org
- Date: Sun, 2 May 2010 11:34:07 -0300 (BRT)
- Subject: Function definition within function
Dear,
Without wishing to be precious and not to cause controversy,
I wonder if the following statements is incorrect or if I am wrong in my interpretation,
or is a hidden feature of the compiler gcc.
In the book 'The C Programming Language' second edition of the authors
Brian W. Kernighan and Dennis M. Ritchie is made the following statement:
'4.8 Block Structure
C is not a block-structured language in the sense of Pascal or similar languages,
because functions defined may not be within other functions.'
In the book 'C The Complete Reference' fourth edition of the author Herbert Schildt
is made the following statement in a note:
'The reason C that is not, technically, the block-structured language is that blockstructured
languages permit procedures or functions to be declared inside other functions or procedures.
However, since C does not allow the creation of functions within functions,
it can not be formally called block-structured.'
Well, I utilize the gcc compiler from the 80s till the present day in various OS environments and
never quite had problems with the definition and use of functions within functions,
so I'm sending in an annex (infpos.c), a function that not only defines a function within another
function as also is the nesting of functions.
Please what is wrong?
Are there any side effect that I'm doing?
See, I am not criticizing this feature, however I believe it is a great
way to encapsulate functions preserving its integrity,
especially in cases like that I'm sending an example that applies various recursions,
both direct and indirect.
PS.: Sorry for the English is not my native language.
Renato Astorino - renato.astorino@hotmail.com
<<<infpos.c>>>
/* -----------------------------------------------------------------------------------------------
The function InFix_PostFix part of my project dealing with metadata in arithmetic expressions.
These expressions are consisted specifiable by another function for this purpose, so the data
entry must meet the following criteria:
- The operands can be started with the letters Innn, Fnnn, Rnnn, Pnnn and Vnnn;
where: nnn is a number that can range from 000 to 999.
- Valid operators:
* Multiplication
/ Division
+ Sum
- Subtraction
- Is allowed the use of parentheses to determine the order of calculation;
- Spaces is not allowed;
- To terminate execution of the testing program use q or Q.
Some examples of expressions and their results:
I001+(F001+(P001+(R001))) <--- (given input)
InFix = I001+(F001+(P001+(R001)))
PostFix = I001F001P001R001+++
(P001+V001)*(I001-R009) <--- (given input)
InFix = (P001+V001)*(I001-R009)
PostFix = P001V001+I001R009-*
((F003-F005)-(P004+P005)+V005)/I006 <--- (given input)
InFix = ((F003-F005)-(P004+P005)+V005)/I006
PostFix = F003F005-P004P005+-V005+I006/
The goal is to create arithmetic expressions in postfix notation for the function calculation.
The compilation option used:
gcc -Wall -ansi infpos.c -o infpos.exe [the environment Windows on
Linux does not use the extension .exe]
----------------------------------------------------------------------------------------------- */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define OPERAND_SIZE 4
char arExprIN[2048],
arExprOUT[2048];
char *ptExprIn,
*ptExprOut;
void InFix_PostFix(void);
int main(void)
{
for ( ; ; )
{
ptExprIn = arExprIN;
ptExprOut = arExprOUT;
memset(arExprIN, ' ', 2048);
scanf("%s",arExprIN);
if ( arExprIN[0] == 'q' || arExprIN[0] == 'Q' )
break;
InFix_PostFix();
printf("
InFix = %s
PostFix = %s
", arExprIN, arExprOUT);
memset(arExprOUT, ' ', 2048);
}
exit(0);
}
void InFix_PostFix(void) /* Function definition InFix_PostFix */
{
void SubExpression(void) /* Function definition SubExpression within the function InFix_PostFix */
{
void OperandSubExpr(void) /* Function definition OperandSubExpr within the function SubExpression */
{
switch (*ptExprIn)
{
case 'I': /* Integers numbers */
case 'F': /* Functions */
case 'R': /* Real numbers */
case 'P': /* Parameters */
case 'V': /* Variables */
strncpy(ptExprOut,ptExprIn, OPERAND_SIZE);
ptExprOut += OPERAND_SIZE;
ptExprIn += OPERAND_SIZE;
break;
case '(':
ptExprIn++;
InFix_PostFix();
ptExprIn++;
}
} /* eof OperandSubExpr */
void OperatorMultDiv(void) /* Function definition OperatorMultDiv within the function SubExpression */
{
char AuxChar;
while ( *ptExprIn == '*' || *ptExprIn == '/' )
{
AuxChar = *ptExprIn;
ptExprIn++;
OperandSubExpr();
*ptExprOut = AuxChar;
ptExprOut++;
OperatorMultDiv();
}
} /* eof OperatorMultDiv */
OperandSubExpr();
OperatorMultDiv();
} /* eof SubExpression */
void OperatorSumSub(void) /* Function definition OperatorSumSub within the function InFix_PostFix */
{
char AuxChar;
while ( *ptExprIn == '+' || *ptExprIn == '-' )
{
AuxChar = *ptExprIn;
ptExprIn++;
SubExpression();
*ptExprOut = AuxChar;
ptExprOut++;
}
} /* eof OperatorSumSub */
if ( *ptExprIn != ' ' && *ptExprIn != ')' )
{
SubExpression();
OperatorSumSub();
}
*ptExprOut = ' ';
} /* eof InFix_PostFix */