Thread: Passing arrays between functions

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    10

    Passing arrays between functions

    Hello everyone, I'm an amateur C programmer working on what every amateur programmer wants to do: make a game. In my case a text adventure, though it's not very far along yet. Anyways, I'm having trouble with passing a pointer to a static array from one function to another. Here's the important parts of my code:

    Code:
    //test_run.c
    #include <stdio.h>
    #include "my_functions.h"
    
    int main() {
       long *array_var=my_function();
       printf("%ld\n",array_var[0][1]);
       return 0;
    }
    
    //my_functions.c
    #include <stdio.h>
    #include "my_functions.h"
    
    long *my_function() {
    
       static long array_var[10][2];
       /*some other declarations*/
    
       /*values are added to the array by use of a while loop*/
    
       return array_var;
    }
    
    //my_functions.h
    long *my_function()
    Here's what happens when I compile "my_functions.c."

    Code:
    >c99 -c my_functions.c -I.
    >warning: return from incompatible pointer type
    If I change the last line of my_functions.c from "return array_var" to "return *array_var", the warning goes away. This doesn't make sense to me.

    And in either case when I try and compile "test_run.c":

    Code:
    >c99 test_run.c my_functions.o -I.
    >error: subscripted value is neither array nor pointer
    Needless to say, when I remove the subscripts it compiles, but I print out some useless number.

    Can someone help me out?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by jtay View Post
    Here's what happens when I compile "my_functions.c."

    Code:
    >c99 -c my_functions.c -I.
    >warning: return from incompatible pointer type
    Because my_function() is returning a pointer to an array of long while in the caller array_var is a simple pointer to long.
    Quote Originally Posted by jtay View Post
    If I change the last line of my_functions.c from "return array_var" to "return *array_var", the warning goes away. This doesn't make sense to me.
    Yep! because now the return type matches the receiving type.
    Quote Originally Posted by jtay View Post
    And in either case when I try and compile "test_run.c":

    Code:
    >c99 test_run.c my_functions.o -I.
    >error: subscripted value is neither array nor pointer
    Needless to say, when I remove the subscripts it compiles, but I print out some useless number.

    Can someone help me out?
    Again, in the caller array_var is a pointer to long so it can't be double-scripted.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    I've fiddled with the code a bit more, but I still can't find what I need to change to get this to work. I don't see how my_function is returning a pointer to an array, since it's declared as long *my_function(). The reason I was trying to say return array_var instead of return *array_var, is because I was under the impression that the name of the array is more or less equivalent to a pointer to the first element in the array. I'm afraid I'm going to need a few more hints from some people before I can figure this out.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    array_var id 2D array - in other words it is array of arrays

    the first element of it array_var[0] is array of 2 longs

    so array_var is converted to pointer to array of 2 longs, which is different type from pointer to long. That's what compiler is trying to tell you
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    Okay, I'm starting to understand how this works. So since my_function is a pointer to long, array_var can only be subscripted once. How would I go about making this so I can subscript it twice? Right now when I print array_var[1], I get the value in row 0, column 1; array_var[2] gives me row 1, column 0; array_var[3] gives me row 1, column 1; etc.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdio.h>
    
    typedef long (*pArr)[2];
    
    pArr test()
    {
        static long arr[10][2] = {{1,2},{3,4}};
    
        return arr;
    }
    
    int main(void)
    {
        pArr newArr = test();
        printf( "[0][0] = %ld, [0][1]=%ld\n"
                "[1][0] = %ld, [1][1]=%ld\n",
                newArr[0][0],newArr[0][1],
                newArr[1][0],newArr[1][1]);
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    Thanks for that! It took me a little while to figure out how to put the typedef in my header file, but my code is working now. I still only understand maybe 70% how this works, but I expect I'll run into this problem several more times, so that should get me some practice. This whole array of pointers to arrays thing is tough to understand the first time around.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM