Help with C Code

Discussion in 'C' started by am2007, Sep 30, 2007.

  1. am2007

    am2007 New Member

    Joined:
    Sep 30, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Can anyone help me with the following code and the questions that follow it?

    LINE Contains
    50 char * b, q, *r;
    200 b=getbuf();
    201 q = *b;
    212 r= anotherfunction(b);
    213-300 /* we want to use ‘q’ and ‘r’ here*/
    2000 char * getbuf()
    2001 {
    2002 char buff[8];
    2003-2050 /* unspecified, buff defined here *./
    2051 return (char *) buff;
    2052 }

    What will be in variable ‘q’ after line 201 is executed? Under what conditions might this not be so?

    Is there an alternative, but equivalent, way to write line 2000? If so, what is it?

    Is getbuf() a reasonable function?

    Will getbuf() execute at all?

    Please comment on line 2051.

    Is getbuf() good practice, and why?

    What line not given should be provided for compilation?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    Same as the value in b provided your getbuf is implemented correctly but it is not. See reason below.

    You can use void* for generalization. Also the character pointer can be passed to the function so we don't use the locale variable address in the callee function.

    Reasonably wrong.

    Yes

    Returning the local variable and so it will be returning the location to the character buffer which is out of scope and so getbuf needs better implementation.

    Nope.

    Use complier to see what error it gets.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice