How to Access Elements in an Array Returned from a Function

Access array returned by a function in php

Since PHP 5.4 it's possible to do exactly that:



On PHP 5.3 or earlier, you'll need to use a temporary variable.

Return array in a function

In this case, your array variable arr can actually also be treated as a pointer to the beginning of your array's block in memory, by an implicit conversion. This syntax that you're using:

int fillarr(int arr[])

Is kind of just syntactic sugar. You could really replace it with this and it would still work:

int fillarr(int* arr)

So in the same sense, what you want to return from your function is actually a pointer to the first element in the array:

int* fillarr(int arr[])

And you'll still be able to use it just like you would a normal array:

int main()
int y[10];
int *a = fillarr(y);
cout << a[0] << endl;

How to make use of an array returned by a function? in C language

Capture the return value in main():

int *primes = findprimes(n);

and use the array:

for (int i = 0; i < some_magic_number; i++)
printf("%d\n", primes[i]);

and remember to free the memory:


In fact, you also need to free numbers in the function.

The some_magic_number needs to be known — how does the main() function know how many elements are in the array? One way to fix that is to pass a pointer to the function that can hold the number of primes:

int *findprimes(int n, int *num_primes)

and use *num_primes = total; in the function before returning.
You could call the function with:

int some_magic_number;
int *primes = findprimes(n, &some_magic_number);

You'd probably choose an alternative name for the variable in main(), but it illustrates my point.

How to use an array that was returned from a method in Java

just store the returned array in a local one

public static void main(String[]args){
int[][]array = fillArray("fileName"); // call the method
// traverse the array using a loop

for(int i=0;i<array.length;i++)
for(int j=0;j<array[i].length;j++)
System.out.println(a[i][j]); // do something with elements


Getting element from PHP array returned by function

PHP cannot do this yet, it's a well-known limitation. You have every right to be wondering "are you kidding me?". Sorry.

If you don't mind generating an E_STRICT warning you can always pull out the first and last elements of an array with reset and end respectively -- this means that you can also pull out any element since array_slice can arrange for the one you want to remain first or last, but that's perhaps taking things too far.

But despair not: the (at this time upcoming) PHP 5.4 release will include exactly this feature (under the name array dereferencing).

Update: I just thought of another technique which would work. There's probably good reason that I 've never used this or seen it used, but technically it does the job.

// To pull out Nth item of array:
list($item) = array_slice(getSomeArray(), N - 1, 1);

Access elements of returned array in Fortran

Further to the answers that deal with SHAPE and logical expressions etc, the general answer to your question "How does one access an element of an array that is returned from a function?" is

  • you assign the expression that has the function reference to an array variable, and then index that array variable.

  • you use the expression that has the function reference as an actual argument to a procedure that takes a dummy array argument, and does the indexing for you.

Consequently, the general answer to your last questions "But do I really have to declare an extra array variable to hold the return value of shape(), or is there some other way to do it?" is "Yes, you do need to declare another array variable" and hence "No, there is no other way".

(Note that reasonable optimising compilers will avoid the need for any additional memory operations/allocations etc once they have the result of the array function, it's really just a syntax issue.)

The rationale for this particular aspect of language design is sometimes ascribed to a need to avoid syntax ambiguity and confusion for array function results that are of character type (they could potentially be indexed and/or substringed - how do you tell what was intended?). Others think it was done this way just to annoy C programmers.

Related Topics

Leave a reply