- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Return array from function in C
Functions in C help the programmers to adapt modular program design. A function can be defined to accept one or more than one arguments, it is able to return a single value to the calling environment. However, the function can be defined to return an array of values. In C, a function can be made to return an array by one of following methods −
- Passing the array as argument and returning the pointer
- Declaring a static array in a function and returning its pointer
- Using malloc() function
Embedding the array inside a struct variable and passing it to a function
We implement these methods to calculate the square, the cube and the square root of a given number.
Pass array by reference
In the following example, we declare an uninitialized array in main() and pass it to a function, along with an integer. Inside the function, the array is filled with the square, cube and square root. The function returns the pointer of this array, using which the values are access and printed in main() function.
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; } int arrfunction(int x, float *arr){ arr[0]=pow(x,2); arr[1]=pow(x, 3); arr[2]=pow(x, 0.5); }
Output
Square of 100: 10000.000000 cube of 100: 1000000.000000 Square root of 100: 10.000000
Return static array
Instead of passing an empty array from main(), we can declare an array inside the called function itself, fill it with the required values, and return its pointer. However, returning a pointer of a local variable is not acceptable, as it points to a variable that no longer exists. Note that a local variable ceases to exist as soon as the scope of the function is over. Hence, we need to use a static array inside the called function (arrfunction) and return its pointer back to main().
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); return arr; }
Output
Square of 100: 10000.000000 cube of 100: 1000000.000000 Square root of 100: 10.000000
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> /* function to generate and return random numbers */ int * getRandom( ) { static int r[10]; int i; /* set the seed */ srand( (unsigned)time( NULL ) ); for ( i = 0; i < 10; ++i) { r[i] = rand(); printf( "r[%d] = %d\n", i, r[i]); } return r; } /* main function to call above defined function */ int main () { /* 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)); } 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
Using malloc() function
The malloc() function is available as a library function in stdlib.h header file. It dynamically allocates a block of memory during the runtime of a program. Normal declaration of variables causes the memory to be allocated at the compile time.
void *malloc(size_t size);
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 example, to store an int data, it must be typecast to int * as follows −
int *x = (int *)malloc(sizeof(int);
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
Square of 16: 256.000000 cube of 16: 4096.000000 Square root of 16: 4.000000
Using array element in struct
In this method, we will declare a struct, inside which there is an float array as its element. The called function (myfunction) declares a struct variable, populates the 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
Return string from function
Using the same approaches, you can pass and return a string to a function. A string in C is an array of char type. In the following example, we pass the string with a pointer, manipulate it inside the function, and return it back to main().
Inside the called function, there is a local string. The string passed is concatenated with the local string before returning.
Example
#include <stdio.h> #include <string.h> #include <stdlib.h> char * hellomsg(char *); int main(){ char * name = "TutorialsPoint"; char *arr = hellomsg(name); printf("%s\n", arr); return 0; } char * hellomsg(char *x){ char *arr = (char *)malloc(50*sizeof(char)); strcpy(arr, "Hello "); strcat(arr, x); return arr; }
Output
Hello TutorialsPoint