function returns int *

This is a discussion on function returns int * within the C Programming forums, part of the General Programming Boards category; I have problems with a function like this: /*Specification*/ int * where (void) { int pos[2]={0,0}; int i, j; for ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Question function returns int *

    I have problems with a function like this:

    /*Specification*/

    int * where (void)
    {
    int pos[2]={0,0};
    int i, j;
    for (i=0;i<12;i++)
    {
    for(j=0;j<12;j++)
    {
    if (t[i][j]=='K')
    {
    pos[0]=i;
    pos[1]=j;
    return pos;
    }
    }
    }
    return pos;
    }

    Borland c 3,1 compiler returns with the message :'warning:Function should return a value".
    What is the problem with this function ?

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    pos is local to your function but you are returning a pointer to it, so the pointer will point to a variable that is no longer in scope. You should either make the variable global, pass the variable into the function or allocate memory on the heap to store it from within the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21