By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Error message : "function returns address of local variable"

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.