473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error - function returns address of local variable

int main(void)
{
char brand[20];

printf("Enter brand: ");
brand = validatebrand();

}

char * validatebrand()
{
char brandName[20];
.....
.......
......
if( (strlen (brandName)) >= 1 && (strlen (brandName)) < 20)
{
return brandName; <-- this is the warning
}
else
{
return NULL;
}
}

Ask user to key in brand, and use a validatebrand() to check whether it's valid or not, if it is valid, return the brandName. But when it is return, and i tried a printf to check what's returned in the main(), i found out tat it's not returned the exact data but the address. How i am going to make the validatebrand() to return the entire valid data after validated? Can anyone point it out?
Thanks
May 27 '07 #1
1 3890
weaknessforcats
9,208 Expert Mod 8TB
Your problem is here:

Expand|Select|Wrap|Line Numbers
  1. char * validatebrand()
  2. {
  3. char brandName[20]; 
  4.  
brandName is an array local to validatebrand(). When validatebrand returns, the brandName array is destroyed. By returning the address of an array that is destoyed, youget the warning.

Your solution is to pass in the array from main(). That would be the
Expand|Select|Wrap|Line Numbers
  1. char brand[20];
  2.  
So you need to change validatebrand() to:

Expand|Select|Wrap|Line Numbers
  1. void validatebrand(char* brandName)
  2. {
  3. etc...
  4.  
May 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
6
by: Alixx Skevington | last post by:
OK why does the following code work in a Windows form with a crystal report viewer but not in a webform. I get the usual CrystalReports.EngineLogONException logon Failed when this is called in...
4
by: octavio | last post by:
Hello members of the comp.lang.c newsgroup. Please I need you help on the following one. Compiling the simple code I'm getting this error message. Why ? Please what's the correct type of the fb...
4
by: Michael Yanowitz | last post by:
I am still new to Python but have used it for the last 2+ months. One thing I'm still not used to is that functions parameters can't change as expected. For example in C, I can have status =...
3
by: Hari Sekhon | last post by:
I've got some code as follows: import re re_regexname = re.compile('abc') ...... ...... various function defs ...... def func1():
9
by: i | last post by:
#include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> char ch; int n,m; void main(); char check(int,int,char); void cash(int,int,char); void debit_card(int,int,char);
13
by: hari | last post by:
Hi all, Is it legal to return a local variable from function. Regards Hari
34
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.