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

  2. sam

    Dan Pop Guest

    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, Jun 25, 2003
    #2
    1. Advertisements

  3. ^^^
    (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.)

    ^^^^^^^^^^^^
    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.
    Ooh, ooh, I know this one!

    "No."

    ;-)
    -Arthur
     
    Arthur J. O'Dwyer, Jun 25, 2003
    #3
  4. 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 A. Odell, Jun 25, 2003
    #4
  5. % cat test.cxx

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

    It is too 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. It's legal C++ too, sure. So?
    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.
    No, since I don't typedef functions I just ignored this.
     
    Mark A. Odell, Jun 25, 2003
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.