Help, getting compiler error warning on returning a temporary character string

Discussion in 'C Programming' started by jt, May 15, 2005.

  1. jt

    jt Guest

    I'm getting a compiler error: warning C4172: returning address of local
    variable or temporary

    Here is the function that I have giving this error: I'm returning a
    temporary char string and its not liking it.
    How can I fix this?

    char *dequeue(struct node **first)
    {
    char temp[1024];
    struct node *p=*first;
    if(*first==NULL)
    {
    printf("\nqueue is empty\n");
    return(0);
    }
    else
    {
    p=*first;
    *first=(*first)->next;
    strcpy(temp,p->data);
    free(p);
    return(temp); <----------------- compiler is not liking this
    }
    }

    Big thanks!
    JT
     
    jt, May 15, 2005
    #1
    1. Advertising

  2. jt

    pete Guest

    jt wrote:
    >
    > I'm getting a compiler error:
    > warning C4172: returning address of local
    > variable or temporary
    >
    > Here is the function that I have giving this error: I'm returning a
    > temporary char string and its not liking it.
    > How can I fix this?
    >
    > char *dequeue(struct node **first)
    > {
    > char temp[1024];


    > return(temp); <----------------- compiler is not liking this
    > }
    > }



    char *dequeue(struct node **first)
    {
    static char temp[1024];
    return temp;
    }

    void dequeue(struct node **first, char *temp)
    {

    }

    --
    pete
     
    pete, May 15, 2005
    #2
    1. Advertising

  3. jt

    Al Bowers Guest

    Re: Help, getting compiler error warning on returning a temporarycharacter string

    jt wrote:
    > I'm getting a compiler error: warning C4172: returning address of local
    > variable or temporary
    >
    > Here is the function that I have giving this error: I'm returning a
    > temporary char string and its not liking it.
    > How can I fix this?
    >
    > char *dequeue(struct node **first)
    > {
    > char temp[1024];
    > struct node *p=*first;
    > if(*first==NULL)
    > {
    > printf("\nqueue is empty\n");
    > return(0);
    > }
    > else
    > {
    > p=*first;
    > *first=(*first)->next;
    > strcpy(temp,p->data);
    > free(p);
    > return(temp); <----------------- compiler is not liking this
    > }
    > }


    Yes, storage for the char array ceases when the function
    returns. Your compiler is informing you that the return
    value is of no value.

    A quick fix would be use of the 'static' keyword. Change
    the declaration of the char array to:

    static char temp[1024];


    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, May 15, 2005
    #3
  4. jt

    jt Guest

    Thank you!
    JT

    "Al Bowers" <> wrote in message
    news:...
    >
    >
    > jt wrote:
    >> I'm getting a compiler error: warning C4172: returning address of local
    >> variable or temporary
    >>
    >> Here is the function that I have giving this error: I'm returning a
    >> temporary char string and its not liking it.
    >> How can I fix this?
    >>
    >> char *dequeue(struct node **first)
    >> {
    >> char temp[1024];
    >> struct node *p=*first;
    >> if(*first==NULL)
    >> {
    >> printf("\nqueue is empty\n");
    >> return(0);
    >> }
    >> else
    >> {
    >> p=*first;
    >> *first=(*first)->next;
    >> strcpy(temp,p->data);
    >> free(p);
    >> return(temp); <----------------- compiler is not liking this
    >> }
    >> }

    >
    > Yes, storage for the char array ceases when the function
    > returns. Your compiler is informing you that the return
    > value is of no value.
    >
    > A quick fix would be use of the 'static' keyword. Change
    > the declaration of the char array to:
    >
    > static char temp[1024];
    >
    >
    > --
    > Al Bowers
    > Tampa, Fl USA
    > mailto: (remove the x to send email)
    > http://www.geocities.com/abowers822/
    >
     
    jt, May 15, 2005
    #4
  5. Re: Help, getting compiler error warning on returning atemporarycharacter string

    Al Bowers wrote:
    >
    > jt wrote:
    > > I'm getting a compiler error: warning C4172: returning address of local
    > > variable or temporary
    > >
    > > Here is the function that I have giving this error: I'm returning a
    > > temporary char string and its not liking it.
    > > How can I fix this?
    > >
    > > char *dequeue(struct node **first)
    > > {
    > > char temp[1024];
    > > struct node *p=*first;
    > > if(*first==NULL)
    > > {
    > > printf("\nqueue is empty\n");
    > > return(0);
    > > }
    > > else
    > > {
    > > p=*first;
    > > *first=(*first)->next;
    > > strcpy(temp,p->data);
    > > free(p);
    > > return(temp); <----------------- compiler is not liking this
    > > }
    > > }

    >
    > Yes, storage for the char array ceases when the function
    > returns. Your compiler is informing you that the return
    > value is of no value.
    >
    > A quick fix would be use of the 'static' keyword. Change
    > the declaration of the char array to:
    >
    > static char temp[1024];
    >
    > --
    > Al Bowers
    > Tampa, Fl USA
    > mailto: (remove the x to send email)
    > http://www.geocities.com/abowers822/


    But also be aware that a future call to dequeue() will clobber what is
    stored there:
    value1 = dequeue(...);
    ...
    value2 = dequeue(...); /* Just clobbered what value1 points to */
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Common User Interface Services
    M/S 2R-94 (206)544-5225
    #! rnews 2715
    Xref: xyzzy sci.energy:130557 sci.energy.hydrogen:80505 sci.environment:395335
    Newsgroups: sci.energy.hydrogen,sci.energy,alt.energy,sci.environment,alt.politics
    Path: xyzzy!nntp
    From: "Fred McGalliard" <>
    Subject: Re: The world's largest solar power station planned for Portugal
    X-Nntp-Posting-Host: e056750.nw.nos.boeing.com
    Message-ID: <>
    X-Mimeole: Produced By Microsoft MimeOLE V6.00.2800.1409
    X-Priority: 3
    X-Msmail-Priority: Normal
    Lines: 31
    Sender: (Boeing NNTP News Access)
    Organization: The Boeing Company
    X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
    References: <> <> <> <1%ife.138$> <> <> <RqTfe.19259$> <> <> <> <3ep2v4F463f2U1
    Date: Mon, 16 May 2005 15:01:32 GMT

    I want you both to step back a bit and look at Don't claim more carefully. I
    think it is interesting, and very jingoistic, Something a politician would
    say at an oil rally, but not as meaningful on close examine. There is no
    question (I think) that some of our panels are making net watt-hours today
    in a strict energy accounting. It is Don's burden that changes this, and his
    odd accounting structure that assures the claim cannot be falsified. But it
    is just this that makes it both interesting, and useless for making our long
    term energy decisions.
    I think we need to examine what "burden" our PV solar structure should carry
    and why before we can safely jump on, or off, the bandwagon.

    "fkasner" <> wrote in message
    news:uqKhe.34$...
    > BobG wrote:
    > > Boy, you are tough Don. The way I read that mantra you keep
    > > repeating.."not one net watthour... has ever been produced" means that
    > > a pv panel from the 70s, sitting in the sun, hasn't reached break even
    > > in 35 years? Way unlikely. Only takes one panel to hit break even and I
    > > have disproven your claim. Time is on my side. (Mick Jagger, 1965).
    > >

    >
    > The key to his claim is the usually well hidden costs for collateral
    > expenses in producing that PV panel and for things that are usually left
    > out that are after manufacture costs such as synchronous inverters if
    > you (as is true in most cases) AC rather than low voltage DC.
    >
    > I don't think he really exaggerates at all.
    >
    > FK
     
    Fred L. Kleinschmidt, May 16, 2005
    #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. Velvet
    Replies:
    9
    Views:
    14,826
    Joerg Jooss
    Jan 19, 2006
  2. qazmlp
    Replies:
    3
    Views:
    387
    Claudio Puviani
    Mar 7, 2004
  3. mimmo
    Replies:
    4
    Views:
    27,964
  4. Dylan
    Replies:
    5
    Views:
    324
    red floyd
    Aug 4, 2005
  5. emeraldsky23

    warning: multi-character character constant error

    emeraldsky23, Jan 15, 2011, in forum: C Programming
    Replies:
    0
    Views:
    1,024
    emeraldsky23
    Jan 15, 2011
Loading...

Share This Page