Thread: XCode Nested Functions not supported

  1. #1
    Registered User Bassglider's Avatar
    Join Date
    Nov 2007
    Posts
    32

    XCode Nested Functions not supported

    I am doing an exercise out of a book which apparently has nested functions.

    I read on http://lists.apple.com/archives/xcod.../msg00271.html

    that xcode does not support them. The compiler says:

    error: nested functions are disabled, use -fnested-functions to re-enable

    Where would I put -fnested-functions to reenable so I can complete this exercise?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    I ran into that same problem but it seems that I was erroneous in my coding in that I didn't put closing }'s on the main function, i use xcode 3.0

  3. #3
    Registered User Bassglider's Avatar
    Join Date
    Nov 2007
    Posts
    32
    I am closing my braces, does anything look off here?

    Code:
    void playerGetsCard(int *numCards, int cards[52], int playerPoints[2])
    {
    int newCard;
    newCard = dealCard(numCards, cards);
    printf("You Draw: ");
    dispCard(newCard, playerPoints);
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    that looks fine, i meant do you have
    Code:
    int main (void){
    
    int i
    return 0;
    }
    
    void myfunct (void)
    {.......
    }

  5. #5
    Registered User Bassglider's Avatar
    Join Date
    Nov 2007
    Posts
    32
    Ohhh, ok gotcha, thanks for that clarification, that compiler won't think its a nested function then correct?

  6. #6
    Registered User Bassglider's Avatar
    Join Date
    Nov 2007
    Posts
    32
    Are you saying that you replace main() with int main(void) { ?

    Even with that change the function I am working on does not get fixed, the main function is here and returns no errors or warnings only the function playersGetCard has the nested errors:

    Code:
    main() {
    int numCards;									/* Equals 52 at beginning of each game */
    int cards[52], playerPoints[2], dealerPoints[2], total[2];
    char ans;										/* for users Hit/Stand or Yes/No Response */
    do { initCardsScreen(cards, playerPoints, dealerPoints, total, &numCards);
    dealerGetsCard(&numCards, cards, dealerPoints);
    printf("\n");
    playerGetsCard(&numCards, cards, playerPoints);
    playerGetsCard(&numCards, cards, playerPoints);
    do {
    ans = getAns("Hit or stand (H/S)? ");
    if (ans == 'H')
    {	playerGetsCard(&numCards, cards, 
    playerPoints);
    }
    } while (ans != 'S');
    totalIt(playerPoints, total, PLAYER);			/* players Total */
    do {
    dealerGetsCard(&numCards, cards, dealerPoints);
    } while (dealerPoints[ACEHIGH] < 17);			/* Dealer Stops */
    totalIt(dealerPoints, total, DEALER);
    												/* Dealer's total */
    findWinner(total);
    ans = getAns("\nPlay again (Y/N)? ");
    } while (ans == 'Y');
    return 0;
    }
    Last edited by Bassglider; 12-10-2007 at 12:36 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Horrible code. That is quite an eyesore >_<
    Indent that mess.
    Also always specify return type, so it should be int main.
    I don't think nested functions are legal in the standard... could be wrong, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User Bassglider's Avatar
    Join Date
    Nov 2007
    Posts
    32
    Yeah, I know, I'm learning, I copied that straight out of a book so I can type it out and learn what everything is doing.

    Do you have any suggested source I can learn flow and tabbing from to make it easier to read?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For every brace {, indent one tab. Then tread over your code and see if you can see additional changes that will make the code more readable to you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well any half-decent code editor has auto-indent capabilities.
    It may even have an indent code feature.

    If you're using some kind of Unix/Linux/BSD clone, there is a popular command line program called 'indent' which also makes a pretty decent job.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User Bassglider's Avatar
    Join Date
    Nov 2007
    Posts
    32
    Ok, how does this look now? I know there are different opinions of ways of indenting, but is this easy to read and done correctly?

    Code:
    main() 
    {
    	int numCards;												/* Equals 52 at beginning of each game */
    	int cards[52], playerPoints[2], dealerPoints[2], total[2];
    	char ans;													/* for users Hit/Stand or Yes/No Response */
    	do 
    	{ 
    		initCardsScreen(cards, playerPoints, dealerPoints, total, &numCards);
    		dealerGetsCard(&numCards, cards, dealerPoints);
    		printf("\n");
    		playerGetsCard(&numCards, cards, playerPoints);
    		playerGetsCard(&numCards, cards, playerPoints);
    		do 
    		{
    			ans = getAns("Hit or stand (H/S)? ");
    			if (ans == 'H')
    				{	
    				playerGetsCard(&numCards, cards, 
    				playerPoints);
    				}
    		} 
    		while (ans != 'S');
    		totalIt(playerPoints, total, PLAYER);						/* players Total */
    		do 
    		{
    			dealerGetsCard(&numCards, cards, dealerPoints);
    		} 
    		while (dealerPoints[ACEHIGH] < 17);						/* Dealer Stops */
    		totalIt(dealerPoints, total, DEALER);
    																/* Dealer's total */
    		findWinner(total);
    		ans = getAns("\nPlay again (Y/N)? ");
    	} 
    	while (ans == 'Y');
    	return 0;
    }

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, the "Nested functions" is another name for "functions defined inside other functions", and is a non-standard extension supported by SOME compilers. It really means that you can do something like this:
    Code:
    int main()
    {
        int foo(int x)
        {
            return x * x;
        }
    
        printf("foo(4) = %d\n", foo(4));
    
        return 0;
    }
    It's useful when porting from a language such as Pascal that has allows "functions inside functions"

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A side note, but main should always return int. And you should also explicity specify a return type for all functions.
    Don't type
    Code:
    main()
    ...instead type...
    Code:
    int main()
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User Bassglider's Avatar
    Join Date
    Nov 2007
    Posts
    32
    oh thanks, that fixed the warning

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Just move the nested function outside of main (or the function it is defined in) and give it a go.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  3. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. Passing data/pointers between functions #2
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 09:49 PM