[newbie]:function returns a pointer to a string

Discussion in 'C Programming' started by uy do, Oct 16, 2003.

  1. uy do

    uy do Guest

    Hi,
    I see a function but I dont white understand a few things. So please help me
    to clear it out.
    The code is as follow
    -------------
    //global variable
    char buf[32768];

    char * myfunc(s1)
    char *s1;
    {
    //definition of the function
    return (buf);
    }
    -------------
    My questions is why don't they define the function as simple as char
    *myfunc(char *s1) ?
     
    uy do, Oct 16, 2003
    #1
    1. Advertising

  2. Greetings.

    In article <>, uy do wrote:
    > char * myfunc(s1)
    > char *s1;
    > {
    > //definition of the function
    > return (buf);
    > }
    > -------------
    > My questions is why don't they define the function as simple as char
    > *myfunc(char *s1) ?


    This is old-style (K&R) C, which predates the ANSI/ISO standards. Nowadays
    the equivalent syntax you suggested is preferred, and all modern compilers
    support it.

    This question is also answered in the FAQ.

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
     
    Tristan Miller, Oct 16, 2003
    #2
    1. Advertising

  3. uy do <> scribbled the following:
    > Hi,
    > I see a function but I dont white understand a few things. So please help me
    > to clear it out.
    > The code is as follow
    > -------------
    > //global variable
    > char buf[32768];


    > char * myfunc(s1)
    > char *s1;
    > {
    > //definition of the function
    > return (buf);
    > }
    > -------------
    > My questions is why don't they define the function as simple as char
    > *myfunc(char *s1) ?


    It's an old K&R style definition. It's more or less the same. Not
    exactly the same though - the way it handles argument promotions
    differs. I don't remember exactly how it differs. K&R style definitions
    are still valid in standard C, but deprecated. <OT>C++ does not allow
    K&R style definitions.</OT>

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "C++ looks like line noise."
    - Fred L. Baube III
     
    Joona I Palaste, Oct 16, 2003
    #3
  4. uy do

    uy do Guest

    Thanks,
    ~Uy
    "Joona I Palaste" <> wrote in message
    news:bmme2k$m6s$...
    > uy do <> scribbled the following:
    > > Hi,
    > > I see a function but I dont white understand a few things. So please

    help me
    > > to clear it out.
    > > The code is as follow
    > > -------------
    > > //global variable
    > > char buf[32768];

    >
    > > char * myfunc(s1)
    > > char *s1;
    > > {
    > > //definition of the function
    > > return (buf);
    > > }
    > > -------------
    > > My questions is why don't they define the function as simple as char
    > > *myfunc(char *s1) ?

    >
    > It's an old K&R style definition. It's more or less the same. Not
    > exactly the same though - the way it handles argument promotions
    > differs. I don't remember exactly how it differs. K&R style definitions
    > are still valid in standard C, but deprecated. <OT>C++ does not allow
    > K&R style definitions.</OT>
    >
    > --
    > /-- Joona Palaste () ------------- Finland --------\
    > \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    > "C++ looks like line noise."
    > - Fred L. Baube III
     
    uy do, Oct 16, 2003
    #4
  5. "uy do" <> wrote:

    <snip>
    >char * myfunc(s1)
    > char *s1;
    > {

    <snip>
    >My questions is why don't they define the function as simple as char
    >*myfunc(char *s1) ?


    It's just an old-style function definition.

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 16, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    729
    Ron Natalie
    Oct 10, 2003
  2. Boris Sargos
    Replies:
    3
    Views:
    402
    John Harrison
    Apr 23, 2004
  3. Fraser Ross
    Replies:
    4
    Views:
    1,097
    Fraser Ross
    Aug 14, 2004
  4. murgan
    Replies:
    6
    Views:
    4,953
    Thad Smith
    Dec 21, 2005
  5. sqweek
    Replies:
    2
    Views:
    430
    Charles Rapp
    Jan 13, 2006
Loading...

Share This Page