LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2008, 12:42 AM   #1
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Rep: Reputation: 15
Smile Help needed :Need to avoid warning


hi i have a code like this.
----------------------------
int* func ();

int main ()
{
int* a;
a = func();
printf ("%d",*a);
return 0;
}

int* func ()
{
int n = 10;
return &n;
}
------------------------------
While compiling this code i will get a warning like "function returns address of local variable"

These are my constraints:

1. I cannot use malloc ()
2. I have to return a pointer from func ()

Is there any way to remove this warning.

Note: since this is a simple program, one may get the output if the program is executed. But it may fail if this is used in large programs. So someone pls help quickly

thanks,
thelink123.
 
Old 12-09-2008, 01:38 AM   #2
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
Make n global and / or static.
 
Old 12-09-2008, 02:35 AM   #3
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Original Poster
Rep: Reputation: 15
Smile

Adding my 3rd constraint

3. n cannot be declared global

Note : The objective of my question is to develop a program that can return the address of a local variable inside a function.!!!

Is this possible????
 
Old 12-09-2008, 04:26 AM   #4
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
As suggested, declare n static. This will preserve its value the func() method completes its job.

Last edited by jay73; 12-09-2008 at 10:41 AM.
 
Old 12-09-2008, 10:38 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
to avoid these munna_dude Programming 11 03-07-2007 11:16 PM
How can I avoid the following [ FAILED ]'s? indienick Linux - Kernel 9 07-24-2006 03:37 PM
How to avoid LD_LIBRARY_PATH stefanlasiewski Programming 5 02-01-2006 08:18 PM
[SOLVED] WARNING: `aclocal-1.6' is needed lord_didger Linux - Software 3 09-03-2005 03:47 PM
Can I get a HOW-TO avoid MCSE jdc2048 General 18 06-23-2003 03:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration