473,671 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,taill e,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 11348
octavio <op**********@g mail.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,taill e,op);
return 0; /* Not implicit in C89 */
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.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**********@g mail.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é
implicitemen t 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,tail le,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,taill e,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)cybers pace.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
3831
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 that error, any pointers will be highly appreciated. -Sergey
15
2536
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
9532
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 in query expression SELECT IIf(IsNull(),"", & Chr(13) & Char(10)) & IIf(IsNull(),"", & Char(13) & Char(10)) & IIf(IsNull(),"", & Char(13) & Char(10)) & IIf(IsNull(),"",] & IIf(IsNull(),"",", ") &
2
1920
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 to first set the value to a cell. I don't have the problem on 2003 server or XP, and I'm wondering if anyone knows of a get around on 2000? Try EApp = CreateObject("Excel.Application")
1
2384
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 url is invoked (it's part of a chain) - you can see it using liveheaders in firefox. What is the best way to debug this? I tried setting debug on HTTPConnection but this doesn't work (from some web posts, setting debug is broken in 2.4). What's...
6
12579
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 FileIOPermission(PermissionState.Unrestricted) f.AllFiles = FileIOPermissionAccess.AllAccess But I still get this error? Any work arounds to this?
3
2466
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 data entry, adding a bunch of new pubs, and all was going swimmingly. But suddenly it has stopped working - now when I try to add a new pub I get an error message "call to undefined function". I have not altered the script in any way since the...
6
1717
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"; $body = 'The first line\n'; $body .= 'The second line.'; $headers = 'From: webmaster@example.com';
6
2927
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 fault) raised in your program"
0
8397
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8919
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8670
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7439
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6230
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.