Return Array From Function in C
Return Array From Function in C
We implement these methods to calculate the square, the cube and the square root
of a given number.
Example
#include <stdio.h>
#include <math.h>
int arrfunction(int, float *);
int main(){
int x=100;
float arr[3];
arrfunction(x, arr);
printf("Square of %d: %f\n", x, arr[0]);
printf("cube of %d: %f\n", x, arr[1]);
printf("Square root of %d: %f\n", x, arr[2]);
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm 1/7
6/16/24, 12:45 PM Return array from function in C
Output
Example
#include <stdio.h>
#include <math.h>
float * arrfunction(int);
int main(){
int x=100, i;
float *arr = arrfunction(x);
printf("Square of %d: %f\n", x, *arr);
printf("cube of %d: %f\n", x, arr[1]);
printf("Square root of %d: %f\n", x, arr[2]);
return 0;
}
float *arrfunction(int x){
static float arr[3];
arr[0]=pow(x,2);
arr[1]=pow(x, 3);
arr[2]=pow(x, 0.5);
https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm 2/7
6/16/24, 12:45 PM Return array from function in C
return arr;
}
Output
Now, consider the following function which will generate 10 random numbers and
return them using an array and call this function as follows −
Example
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/* a pointer to an int */
int *p;
int i;
p = getRandom();
for ( i = 0; i < 10; i++ ) {
printf( "*(p + %d) : %d\n", i, *(p + i));
https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm 3/7
6/16/24, 12:45 PM Return array from function in C
}
return 0;
}
When the above code is compiled together and executed, it produces the following
result −
Output
r[0] = 2110147662
r[1] = 1427553496
r[2] = 1243625529
r[3] = 857484361
r[4] = 513293736
r[5] = 964923407
r[6] = 36104419
r[7] = 1248464892
r[8] = 1838450240
r[9] = 2096489563
*(p + 0) : 2110147662
*(p + 1) : 1427553496
*(p + 2) : 1243625529
*(p + 3) : 857484361
*(p + 4) : 513293736
*(p + 5) : 964923407
*(p + 6) : 36104419
*(p + 7) : 1248464892
*(p + 8) : 1838450240
*(p + 9) : 2096489563
The malloc() function returns a generic void pointer. To assign values of a certain
data type in the allocated memory, it must be typecast to the required type. For
https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm 4/7
6/16/24, 12:45 PM Return array from function in C
Let us allocate a block of memory sufficient to store three float values corresponding
to square, cube and square root of a number, and return the float pointer to main(),
inside which the computed values are displayed.
Example
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
float * arrfunction(int);
int main(){
int x=16, i;
float *arr = arrfunction(x);
printf("Square of %d: %f\n", x, arr[0]);
printf("cube of %d: %f\n", x, arr[1]);
printf("Square root of %d: %f\n", x, arr[2]);
return 0;
}
float *arrfunction(int x){
float *arr = (float *)malloc(3*sizeof(float));
arr[0]=pow(x,2);
arr[1]=pow(x, 3);
arr[2]=pow(x, 0.5);
return arr;
}
Output
https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm 5/7
6/16/24, 12:45 PM Return array from function in C
array element with square, cube and the square root of the argument received by it,
and returns it to the main() function.
Example
#include <stdio.h>
#include <math.h>
struct mystruct{
float arr[3];
};
struct mystruct myfunction(int x);
int main(){
int x = 9;
struct mystruct s = myfunction(x);
printf("Square of %d: %f\n", x, s.arr[0]);
printf("cube of %d: %f\n", x, s.arr[1]);
printf("Square root of %d: %f\n", x, s.arr[2]);
return 0;
}
struct mystruct myfunction(int x){
struct mystruct s1;
s1.arr[0]=pow(x,2);
s1.arr[1]=pow(x,3);
s1.arr[2]=pow(x, 0.5);
return s1;
}
Output
Square of 9: 81.000000
cube of 9: 729.000000
Square root of 9: 3.000000
Inside the called function, there is a local string. The string passed is concatenated
with the local string before returning.
https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm 6/7
6/16/24, 12:45 PM Return array from function in C
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Output
Hello TutorialsPoint
https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm 7/7