What does the following syntax mean (function declared inside a function)

This is a discussion on What does the following syntax mean (function declared inside a function) within the C Programming forums, part of the General Programming Boards category; Code: char ** parse(char *line) { char *newstr(); . . . . } char *newstr(char *s, int l) { . ...

  1. #1
    vyn
    vyn is offline
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    What does the following syntax mean (function declared inside a function)

    Code:
    char ** parse(char *line) {
    	char	*newstr();
    	.
    	.
    	.
    	.
    }
    
    char *newstr(char *s, int l) {
    	.
    	.
    }
    newstr is declared inside the function parse.

    In the list of function prototypes in the beginning of the file, newstr is the only one undeclared.

    What does it mean? Does it act like a function prototype? What's the advantage? How come newstr declared inside parse does not contain any parameters?

    [Edit]
    Might as well link where I found the source code
    http://wps.prenhall.com/wps/media/ob...de/ch09/smsh.h
    http://wps.prenhall.com/wps/media/ob...09/splitline.c
    Last edited by vyn; 04-10-2009 at 10:29 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    It is a function prototype.

    While I think it is technically allowed, there is nothing to gain from declaring a function in another as you could not define it inside the function either, effectively where the function name is in scope.

    There are better string libraries out there if you need one.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by whiteflags View Post
    It is a function prototype.

    While I think it is technically allowed, there is nothing to gain from declaring a function in another as you could not define it inside the function either, effectively where the function name is in scope.

    There are better string libraries out there if you need one.
    It could be there to make sure that only parse has access to newstr (like class member functions in Sepples)
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    That is not how it's done in C. Declare a function with the static keyword if you want to restrict access to a translation unit.

    static char * newstring ( void );

    But that's as close as it gets. Otherwise, if a function name is not in global scope it should be undeclared.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is perfectly legal to do this, and it's making it clear to any reader that this function is not used anywhere else [at least not BEFORE the function has been defined!] since the newstr prototype goes out of scope when at the end of parse.

    --
    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.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I understand that it is legal, but is there any legitimate reason to omit the argument types from the function declaration?

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by R.Stiltskin View Post
    I understand that it is legal, but is there any legitimate reason to omit the argument types from the function declaration?
    i think its an error not to write the argument types while declaring the function.and also no arguments while declaration means it can take any unknown number of arguments while actually in the definition it is taking two parameters.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by BEN10
    i think its an error not to write the argument types while declaring the function.and also no arguments while declaration means it can take any unknown number of arguments while actually in the definition it is taking two parameters.
    I think that by "error" you mean "mistake" or "bad practice" rather than something that can be interpreted as "compile error".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21