Error message : "function returns address of local variable"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • octavio

    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;
    }

  • Christopher Benson-Manica

    #2
    Re: Error message : &quot;functi on returns address of local variable&quot;

    octavio <oparravicini@g mail.com> wrote:
    [color=blue]
    > Why ? Please what's the correct type of the fb function ?[/color]

    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.
    [color=blue]
    > and.c:34: attention : function returns address of local variable[/color]

    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.
    [color=blue]
    > #include<stdio. h>[/color]
    [color=blue]
    > int main (void){[/color]
    [color=blue]
    > 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;[/color]
    [color=blue]
    > *r=fb(a,b,taill e,op);[/color]

    return 0; /* Not implicit in C89 */
    [color=blue]
    > }[/color]

    --
    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.

    Comment

    • Mark McIntyre

      #3
      Re: Error message : &quot;functi on returns address of local variable&quot;

      On 14 Dec 2005 11:36:18 -0800, in comp.lang.c , "octavio"
      <oparravicini@g mail.com> wrote:
      [color=blue]
      >Compiling the simple code I'm getting this error message.
      >Why ? Please what's the correct type of the fb function ?[/color]
      [color=blue]
      >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 »[/color]

      you don't mention this error, which is also serious...
      [color=blue]
      >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);[/color]

      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().
      [color=blue]
      >int * fb(int a[5],int b[5],int taille,int op){[/color]

      this function returns a pointer to an int.
      [color=blue]
      >int res[taille]; /*res c'est le tableau résultat */[/color]

      res is a local variable. Once fb is finished, this variable is thrown
      away. So its address points to nothing.
      [color=blue]
      > return res;[/color]

      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 =----

      Comment

      • freeposte@gmail.com

        #4
        Re: Error message : &quot;functi on returns address of local variable&quot;

        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

        Comment

        • Christopher Benson-Manica

          #5
          Re: Error message : &quot;functi on returns address of local variable&quot;

          freeposte@gmail .com wrote:
          [color=blue]
          > 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.[/color]

          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...
          [color=blue]
          > 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);[/color]

          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 */
          [color=blue]
          > }[/color]

          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.

          Comment

          Working...