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

  1. #1
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    596
    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
    28,413
    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".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 04:09 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM