473,395 Members | 2,796 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,395 software developers and data experts.

Error message : "function returns address of local variable"

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 function ?

Thank You.
Octavio

and.c:15: attention : type mismatch with previous implicit declaration
and.c:11: attention : previous implicit declaration of `fb'
and.c:15: attention : « fb » a été précédemment déclaré
implicitement comme retournant un « int »
and.c: Dans la fonction « fb »:
and.c:34: attention : function returns address of local variable

#include<stdio.h>

int main (void){

int taille = 5;
int a[5]={1,1,0,0,0};/*tableaux de test*/
int b[5]={1,0,1,0,0};
int *r; int i;
int op=0;

*r=fb(a,b,taille,op);

}

int * fb(int a[5],int b[5],int taille,int op){

int mat_and[2][2]={0,0,0,1};

int i;
int res[taille]; /*res c'est le tableau résultat */

switch(op)
{
case 0:
{ /*opération AND colonne par colonne des 2 vecteurs */

for(i=0; i<taille; i++){
res[i]=mat_and[a[i]][b[i]];
}
break;
}

} //switch end
return res;
}

Dec 14 '05 #1
4 11327
octavio <op**********@gmail.com> wrote:
Why ? Please what's the correct type of the fb function ?
You used it before you defined it. In C89, functions without
prototypes in scope are implicitly defined to return int; therefore,
the compiler assumes that fb() is a function that returns int, and
when you define it otherwise later, it complains. Move the definition
or include a prototype for fb() before you use it.
and.c:34: attention : function returns address of local variable
Do NOT ignore this warning. Where, exactly, do you think that address
is going to point after the function returns? It certainly isn't
going to point to memory that you have any right to expect to use.
#include<stdio.h> int main (void){ int taille = 5;
int a[5]={1,1,0,0,0};/*tableaux de test*/
int b[5]={1,0,1,0,0};
int *r; int i;
int op=0; *r=fb(a,b,taille,op);
return 0; /* Not implicit in C89 */
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 14 '05 #2
On 14 Dec 2005 11:36:18 -0800, in comp.lang.c , "octavio"
<op**********@gmail.com> wrote:
Compiling the simple code I'm getting this error message.
Why ? Please what's the correct type of the fb function ? and.c:15: attention : type mismatch with previous implicit declaration
and.c:11: attention : previous implicit declaration of `fb'
and.c:15: attention : « fb » a été précédemment déclaré
implicitement comme retournant un « int »
you don't mention this error, which is also serious...
and.c:34: attention : function returns address of local variable

#include<stdio.h>

int main (void){

int taille = 5;
int a[5]={1,1,0,0,0};/*tableaux de test*/
int b[5]={1,0,1,0,0};
int *r; int i;
int op=0;

*r=fb(a,b,taille,op);
here, you call a function fb() but the compiler doesn't yet have a
defintion for it. Either prototype the fn before main, or move the
entire function definition before main. The former works fine. Just
insert
int * fb(int a[5],int b[5],int taille,int op);

anywhere before the start of main().
int * fb(int a[5],int b[5],int taille,int op){
this function returns a pointer to an int.
int res[taille]; /*res c'est le tableau résultat */
res is a local variable. Once fb is finished, this variable is thrown
away. So its address points to nothing.
return res;


Here you return res, converted to a pointer. You can't do this, since
res is a local variable that has been deleted once fb ended..

(By the way, your compiler also should also have complained that res
was an incompatible type to int*. Whatever you may have heard,
pointers and arrays are NOT the same in C...)

Anyway, you need to return the actual data. The usual way to do that
is to pass in a parameter to hold it, and fill it as you go along.

void fb(int a[5], int b[5]., int taille, int op, int result[10]);

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 14 '05 #3
Christopher , I just put the entire fb function before the main
procedure and I did change the return type also to "return *res" and it
looks to works fine.
Many thanks.

Octavio

Dec 15 '05 #4
fr*******@gmail.com wrote:
Christopher , I just put the entire fb function before the main
procedure and I did change the return type also to "return *res" and it
looks to works fine.
Unless I'm missing something, return *res should not work. *res is an
int, and the function returns an int *. What you really want, I'm
gathering, is to return an array you've dynamically allocated with
malloc(). Also, you still have at least one error in the code you
originally posted...
int main (void){

int taille = 5;
int a[5]={1,1,0,0,0};/*tableaux de test*/
int b[5]={1,0,1,0,0};
int *r; int i;
int op=0;

*r=fb(a,b,taille,op);
Here you dereference r, which you have not pointed at anything.
That's a Bad Thing. You want

r=fb(a,b,taille,op); /* Return a pointer, r is a pointer */
}


Take a look at malloc() and how it applies to fb(). HTH.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sergey | last post by:
Hi, Randomly I get an error "Function returned |" while trying to upload a file in ASP application, use FileUp server- side component from SoftWare Artisans ... can't find any references to...
15
by: Robert Allan Schwartz | last post by:
I've heard the phrase "Miranda function" used to refer to the 6 member functions supplied by the compiler: default constructor copy constructor destructor operator= operator& const operator&
5
by: deko | last post by:
After developing an MDB in Access 2003 on WS03, then making it into an MDE and deploying it on a WinXP box with Access 2003 installed, I get this error: Function is not available in expressions...
2
by: David Reynolds | last post by:
Hi Everybody, I have a vb.net webform where I create the Excel application/workbook/worksheet. On Windows 2000 server for some reason, I keep getting an error("Bad variable type") when trying...
1
by: joemynz | last post by:
Help please with a URLError. Invoking a url that works in Firefox and IE results in a "urlerror 7, no address ..." in python. I need to debug why. Traceback is below. There's a redirect when the...
6
by: Rob R. Ainscough | last post by:
I'm not sure why I'm getting this error when searching thru my local hard drives using My.Computer.FileSystem.GetFiles? I've got FileIOPermissions set in code: Dim f As New...
3
by: news | last post by:
Hi all, hope you can help me. I have recently set up a mysql database of local pubs, with a web form to add new listings (using PHP to generate an INSERT query). I've just been doing a bit of...
6
by: Kurda Yon | last post by:
Hi everybody, I have a problem with the function "mail". It returns "true" but supposed recipients receive nothing. My code is as follows: $to = $form; $subject = "E-mail Verification";...
6
by: hadad.yaniv | last post by:
Hello, i am new to c++, i hav a vector of typed object: vector<Man*People; When i do a second pushback, even for the same object the program crash say: "An Access Violation (Segmentation...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.