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

J

jt

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
 
P

pete

jt said:
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)
{

}
 
A

Al Bowers

jt said:
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];
 
J

jt

Thank you!
JT

Al Bowers said:
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 address removed) (remove the x to send email)
http://www.geocities.com/abowers822/
 
F

Fred L. Kleinschmidt

Al said:
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 address 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.environment,alt.politics
Path: xyzzy!nntp
From: "Fred McGalliard" <[email protected]>
Subject: Re: The world's largest solar power station planned for Portugal
X-Nntp-Posting-Host: e056750.nw.nos.boeing.com
Message-ID: <[email protected]>
X-Mimeole: Produced By Microsoft MimeOLE V6.00.2800.1409
X-Priority: 3
X-Msmail-Priority: Normal
Lines: 31
Sender: (e-mail address removed) (Boeing NNTP News Access)
Organization: The Boeing Company
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
References: <[email protected]> <[email protected]> <[email protected]> <1%[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> <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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top