C function override

Discussion in 'C Programming' started by sam, Jun 24, 2003.

  1. sam

    sam Guest

    I got these two functions and questions from an employer.
    I know pointer and pointer array, but how can I implement without modifying
    original implementation.
    Could someone help me on this question? Does C supports function override?



    void func(){
    char * p;
    p=test();
    }
    char * test(){
    char buf[8];

    /*Here*/

    return (char *) buf;
    }

    Question:
    How, exactly, could one get a second 'char *' to use back from this
    function? Be specific in terms of the exact syntax needed. Another way to
    state this question is how can this function be modified to return a 'char *
    ' from the function, and an additional 'char *' value in one function call.
    Please make sure that your answer will work even if the size of the char *
    desired is not known in the outside calling function. Avoid C++ syntax.
    Include statements in called and calling functions. Use good programming
    practice. Although alternatives are acceptable, for this question, please
    include an answer which maintains the original return type
    sam, Jun 24, 2003
    #1
    1. Advertising

  2. sam

    Dan Pop Guest

    Re: C function override

    In <> (Liu Jin) writes:

    >"sam" <> wrote in message news:<BW3Ka.88222$>...
    >> I got these two functions and questions from an employer.
    >> I know pointer and pointer array, but how can I implement without modifying
    >> original implementation.
    >> Could someone help me on this question? Does C supports function override?
    >>
    >>
    >>
    >> void func(){
    >> char * p;
    >> p=test();
    >> }
    >> char * test(){
    >> char buf[8];
    >>
    >> /*Here*/
    >>
    >> return (char *) buf;
    >> }
    >>
    >> Question:
    >> How, exactly, could one get a second 'char *' to use back from this
    >> function? Be specific in terms of the exact syntax needed. Another way to
    >> state this question is how can this function be modified to return a 'char *
    >> ' from the function, and an additional 'char *' value in one function call.

    >
    >The buf would be automaticaly freeed by system when return from test,
    >you should use malloc in test instead.


    Nope, a mere static is enough. And the cast is downright stupid, of
    course.

    Also, test() should be defined *before* func(), or a declaration for
    test() must be in scope when test() is called from func() (otherwise the
    compiler will complain about an int being assigned to a pointer).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jun 25, 2003
    #2
    1. Advertising

  3. On Tue, 24 Jun 2003, Dann Corbit wrote:
    >
    > "sam" <> wrote in message
    > news:BW3Ka.88222$...
    > > I got these two functions and questions from an employer.

    >
    > Perhaps as a simple interview question. SmElLs like HoMeWoRk.


    > > Could someone help me on this question?


    > > Question:
    > > How, exactly, could one get a second 'char *' to use back from this
    > > function?


    > > Another way to state this question is how can this function be
    > > modified to return a 'char *' from the function, and an additional
    > > 'char *' value in one function call.

    ^^^
    (In _another_ function call? Or in the _same_ function call?
    Rhetorical question, unless the OP is actually going to give the
    problem a try before posting again. Just noting that the problem
    statement is practically ungrammatical as it stands, and certainly
    nonsensical.)


    > > Avoid C++ syntax.

    >
    > Hate to say it, but that's not so easy, since C is nearly a subset of C++,
    > but it can be done with a bit of elbow grease.
    >
    > This should do it:
    > char * foo()

    ^^^^^^^^^^^^
    Isn't this line C++? Would

    typedef char *compl;
    compl foo()

    be a proper fix? :)

    > {

    ^
    This line is also C++.
    Unfortunately, I don't see any way
    to make it not C++, other than simply
    combining it with one of the adjacent
    lines.

    > char *new;
    > new = calloc(1,1);
    > return new;
    > }
    >


    > > Although alternatives are acceptable, for this question, please
    > > include an answer which maintains the original return type

    >
    > Would this do:
    >
    > typedef struct tag_bar {
    > int farbarczar[12];
    > } bar;
    >
    > char *foo(void)
    > {
    > bar foob;
    > return ((char *) ((int) &foob));
    > }


    Ooh, ooh, I know this one!

    "No."

    ;-)
    -Arthur
    Arthur J. O'Dwyer, Jun 25, 2003
    #3
  4. "Arthur J. O'Dwyer" <> wrote in
    news:p:


    >> char * foo()

    > ^^^^^^^^^^^^
    > Isn't this line C++?


    No. This is a function that takes no args and returns a pointer to char.
    I'd define it as char *foo(void) { ... } though. Fully legal C.

    --
    - Mark ->
    --
    Mark A. Odell, Jun 25, 2003
    #4
  5. On Wed, 25 Jun 2003, Mark A. Odell wrote:

    > "Arthur J. O'Dwyer" <> wrote...
    > [Dann Corbitt wrote:]
    > >> char * foo()

    > > ^^^^^^^^^^^^
    > > Isn't this line C++?

    >
    > No.


    % cat test.cxx

    char *foo()
    {
    return 0;
    }
    % g++ -W -Wall -ansi -pedantic -c test.cxx
    %

    It is too C++!

    > This is a function that takes no args and returns a pointer to char.
    > I'd define it as char *foo(void) { ... } though. Fully legal C.


    Well, *of course* it's fully legal C. I was merely pointing out that it's
    also legal C++, and Dann was trying to write the function without using
    any C++ constructs!

    Does that really mean that you didn't see *anything* suspicious about
    the suggested "fix"?

    typedef char *compl;
    compl foo()

    ;)
    -Arthur
    Arthur J. O'Dwyer, Jun 25, 2003
    #5
  6. "Arthur J. O'Dwyer" <> wrote in
    news:p:

    >> >> char * foo()
    >> > ^^^^^^^^^^^^
    >> > Isn't this line C++?

    >>
    >> No.

    >
    > % cat test.cxx
    >
    > char *foo()
    > {
    > return 0;
    > }
    > % g++ -W -Wall -ansi -pedantic -c test.cxx
    > %
    >
    > It is too C++!


    It's legal C++ too, sure. So?

    >> This is a function that takes no args and returns a pointer to char.
    >> I'd define it as char *foo(void) { ... } though. Fully legal C.

    >
    > Well, *of course* it's fully legal C. I was merely pointing out that
    > it's also legal C++, and Dann was trying to write the function without
    > using any C++ constructs!


    I'm not sure that it is possible. I mean, 'char', is a valid C++ keyword
    so it depends on what you mean by "construct" I guess.

    > Does that really mean that you didn't see *anything* suspicious about
    > the suggested "fix"?


    No, since I don't typedef functions I just ignored this.

    --
    - Mark ->
    --
    Mark A. Odell, Jun 25, 2003
    #6
    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. Angus Comber
    Replies:
    3
    Views:
    808
    John Harrison
    Apr 10, 2004
  2. Angus Comber
    Replies:
    3
    Views:
    875
    John Harrison
    Apr 11, 2004
  3. David
    Replies:
    2
    Views:
    467
    David
    Jun 27, 2004
  4. Replies:
    2
    Views:
    892
    Bengt Richter
    Aug 1, 2005
  5. Peng Yu
    Replies:
    4
    Views:
    335
    peter koch
    Sep 20, 2008
Loading...

Share This Page