Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help, getting compiler error warning on returning a temporary character string

Reply
Thread Tools

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

 
 
jt
Guest
Posts: n/a
 
      05-15-2005
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


 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      05-15-2005
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
 
Reply With Quote
 
 
 
 
Al Bowers
Guest
Posts: n/a
 
      05-15-2005


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: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
jt
Guest
Posts: n/a
 
      05-15-2005
Thank you!
JT

"Al Bowers" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> 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: (E-Mail Removed) (remove the x to send email)
> http://www.geocities.com/abowers822/
>



 
Reply With Quote
 
Fred L. Kleinschmidt
Guest
Posts: n/a
 
      05-16-2005


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: (E-Mail Removed) (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.envi ronment,alt.politics
Path: xyzzy!nntp
From: "Fred McGalliard" <(E-Mail Removed)>
Subject: Re: The world's largest solar power station planned for Portugal
X-Nntp-Posting-Host: e056750.nw.nos.boeing.com
Message-ID: <(E-Mail Removed)>
X-Mimeole: Produced By Microsoft MimeOLE V6.00.2800.1409
X-Priority: 3
X-Msmail-Priority: Normal
Lines: 31
Sender: (E-Mail Removed) (Boeing NNTP News Access)
Organization: The Boeing Company
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
References: <(E-Mail Removed) .com> <(E-Mail Removed)> <(E-Mail Removed). com> <1%ife.138$(E-Mail Removed)> <(E-Mail Removed) .com> <(E-Mail Removed)> <RqTfe.19259$(E-Mail Removed)> <(E-Mail Removed) .com> <(E-Mail Removed)> <(E-Mail Removed) .com> <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" <(E-Mail Removed)> wrote in message
news:uqKhe.34$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string with control character in caret notation to realcontrol character string. Bart Vandewoestyne C Programming 8 09-25-2012 12:41 PM
warning: multi-character character constant error emeraldsky23 C++ 0 01-15-2011 11:41 PM
8 bit character string to 16 bit character string Brand Bogard C Programming 8 05-28-2006 05:05 PM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM
returning const reference and compiler warning Dylan C++ 5 08-04-2005 04:11 PM



Advertisments