Error message : "function returns address of local variable"

O

octavio

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=mat_and[a][b];
}
break;
}

} //switch end
return res;
}
 
C

Christopher Benson-Manica

octavio said:
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 */
 
M

Mark McIntyre

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]);
 
F

freeposte

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
 
C

Christopher Benson-Manica

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top