1

If this is possible:

#include <stdio.h>
#include <process.h>

#define SIZE 5

void PassingArray(int arr[])
{
    int i=0;
    for(i=0 ; i<SIZE ; i++)
    {
        printf("%d, ", arr[i]);
    }
    printf("\n");
}

main()
{
    int myIntArray[5] = {1, 2, 3, 4, 5};

    PassingArray(myIntArray);

    system("PAUSE");
}

Then why the following is illegal?

#include <stdio.h>
#include <process.h>

#define SIZE 5

int ReturningArray()[]
{
    int myIntArray[5] = {1, 2, 3, 4, 5};

    return myIntArray;
}

main()
{
    int myArray[] = ReturningArray();

    system("PAUSE");
}
3
  • Are you trying to return a pointer to an int array from your second version of ReturningArray? That signature does not look right..? Commented Dec 10, 2011 at 11:19
  • As I passed an integer array, I am trying to return an integer array. Commented Dec 10, 2011 at 11:22
  • 1
    A function prototype to return an array of int would look something like int* ReturningArray, but you would have to manage malloc and freeing the memory yourself. I suggest you check out cslibrary.stanford.edu/102/PointersAndMemory.pdf Commented Dec 10, 2011 at 11:25

5 Answers 5

4

You're not returning an int, but you're returning the array. This is the same value as &myIntArray[0]. int ReturningArray()[] is not a valid function prototype.

Sign up to request clarification or add additional context in comments.

2 Comments

Why is it made illegal? What about this: int [] ReturningArray(); ?
2

There's multiple reasons why this doesn't work.

The first is simply that it's prohibited by the language - the return type of a function shall not be an array (it also can't be a function).

The second is that even if you were allowed to declare ReturningArray as you do, you could never write a valid return statement in that function - an expression with array type that is not the subject of the unary & or sizeof operators evaluates to a pointer to the first element of the array, which no longer has array type. So you can't actually make return see an array.

Thirdly, even if we somehow had a function returning an array type, you couldn't use that return value as the initialiser of an array variable - the return value would again evaluate to a pointer to the first element of the array: in this case a pointer to int, and a pointer to int isn't a suitable initialiser for an array of int.

Comments

2

There are several problems with this code.

  1. You are placing the brackets at the wrong place. Instead of

    int ReturningArray()[]
    

    it should be

    int* ReturningArray()
    
  2. You are returning a local variable. Local variables only exist during the execution of the function and will be removed afterwards.

In order to make this work you will have to malloc the int array and return the pointer to the array:

#include <stdio.h>
#include <malloc.h>

#define SIZE 5

int* ReturningArray()
{
    int *myIntArray = (int *)malloc(SIZE * sizeof(int));
    myIntArray[0] = 1;
    myIntArray[1] = 2;
    myIntArray[2] = 3;
    myIntArray[3] = 4;
    myIntArray[4] = 5;
    return myIntArray;
}

int main(void)
{
        int i;
        int* myArray = ReturningArray();
        for(i=0;i<SIZE;i++) { 
                printf("%d\n", myArray[i]); 
        }
        free(myArray); // free the memory again
        system("PAUSE");
        return 0;
}

4 Comments

very clear answer... and I love your comment on that other one.
"Arrays are just pointers to a heap space where the values are stored.". That's not true. Arrays are arrays. It's possible to have a pointer to the first element of an array, and using the name of an array in most contexts results in that pointer, but that pointer is not an array. Finally, you cannot pass arrays as parameters, the syntax that looks like it passes an array in fact passes a pointer, but that pointer is still not an array.
@SteveJessop: 1. Indeed, that was a incorrect - 2. Where in the world did I say that you can pass arrays as parameters?
@halfdan: I don't think you did, but that syntax is one of the things that makes some people (including the questioner) confuse arrays with pointers -- the fact that there exists some syntax that appears to pass an array but actually passes a pointer.
2

PassingArray is legal, but it does not pass an array. It passes a pointer to the first element of an array. void PassingArray(int arr[]) is a confusing synonym for void PassingArray(int *arr). You can't pass an array by value in C.

ReturningArray is not allowed, you can't return an array by value in C either. The usual workaround is to return a struct containing an array:

typedef struct ReturnArray {
    int contents[5];
} ReturnArray;

ReturnArray ReturningArray()
{
    ReturnArray x = {{1, 2, 3, 4, 5}};
    return x;
}

Arrays are second-class citizens in C, the fact that they can't be passed or returned by value is historically related to the fact that they can't be copied by assignment. And as far as I know, the reason for that is buried in the early development of C, long before it was standardized, when it wasn't quite decided how arrays were going to work.

Comments

0

You can't return array from a function, but It is possible that you can declare a function returning a (reference in C++) or pointer to array as follows:

int myIntArray[] = {1, 2, 3, 4, 5};

int (*ReturningArray())[sizeof(myIntArray)/sizeof(int)] {

    return &myIntArray;
}

1 Comment

@Alf: I downvoted before the answer was edited, at the point when it was nonsensical. I've removed my downvote now. (Incidentally, I disagree with the idea of counter-voting; you should be upvoting if you believe the answer to be useful!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.