Što je niz?
Niz je vrsta podatkovne strukture koja pohranjuje homogenu zbirku podataka fiksne veličine. Ukratko, možemo reći da je niz skup varijabli istog tipa.
math.pow java
Na primjer, ako želimo deklarirati 'n' broj varijabli, n1, n2...n., ako kreiramo sve te varijable pojedinačno, onda to postaje vrlo zamoran zadatak. U tom slučaju stvaramo niz varijabli istog tipa. Svakom elementu niza može se pristupiti pomoću indeksa elementa.
Pogledajmo prvo kako proslijediti jednodimenzionalni niz funkciji.
Prosljeđivanje niza u funkciju#include void getarray(int arr[]) { printf('Elements of array are : '); for(int i=0;i<5;i++) { printf('%d ', arr[i]); } int main() arr[5]="{45,67,34,78,90};" getarray(arr); return 0; < pre> <p>In the above program, we have first created the array <strong>arr[]</strong> and then we pass this array to the function getarray(). The <strong>getarray()</strong> function prints all the elements of the array arr[].</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/16/return-an-array-c.webp" alt="Return an Array in C"> <p> <strong>Passing array to a function as a pointer</strong> </p> <p>Now, we will see how to pass an array to a function as a pointer.</p> <pre> #include void printarray(char *arr) { printf('Elements of array are : '); for(int i=0;i<5;i++) { printf('%c ', arr[i]); } int main() char arr[5]="{'A','B','C','D','E'};" printarray(arr); return 0; < pre> <p>In the above code, we have passed the array to the function as a pointer. The function <strong>printarray()</strong> prints the elements of an array.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/16/return-an-array-c-2.webp" alt="Return an Array in C"> <h4>Note: From the above examples, we observe that array is passed to a function as a reference which means that array also persist outside the function.</h4> <p> <strong>How to return an array from a function</strong> </p> <p> <strong>Returning pointer pointing to the array</strong> </p> <pre> #include int *getarray() { int arr[5]; printf('Enter the elements in an array : '); for(int i=0;i<5;i++) { scanf('%d', &arr[i]); } return arr; int main() *n; n="getarray();" printf(' elements of array are :'); for(int i="0;i<5;i++)" printf('%d', n[i]); 0; < pre> <p>In the above program, <strong>getarray()</strong> function returns a variable 'arr'. It returns a local variable, but it is an illegal memory location to be returned, which is allocated within a function in the stack. Since the program control comes back to the <strong>main()</strong> function, and all the variables in a stack are freed. Therefore, we can say that this program is returning memory location, which is already de-allocated, so the output of the program is a <strong>segmentation fault</strong> .</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/16/return-an-array-c-3.webp" alt="Return an Array in C"> <p> <strong>There are three right ways of returning an array to a function:</strong> </p> <ul> <tr><td>Using dynamically allocated array</td> </tr><tr><td>Using static array</td> </tr><tr><td>Using structure</td> </tr></ul> <img src="//techcodeview.com/img/c-tutorial/16/return-an-array-c-4.webp" alt="Return an Array in C"> <p> <strong>Returning array by passing an array which is to be returned as a parameter to the function.</strong> </p> <pre> #include int *getarray(int *a) { printf('Enter the elements in an array : '); for(int i=0;i<5;i++) { scanf('%d', &a[i]); } return a; int main() *n; a[5]; n="getarray(a);" printf(' elements of array are :'); for(int i="0;i<5;i++)" printf('%d', n[i]); 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/16/return-an-array-c-5.webp" alt="Return an Array in C"> <p> <strong>Returning array using malloc() function.</strong> </p> <pre> #include #include int *getarray() { int size; printf('Enter the size of the array : '); scanf('%d',&size); int *p= malloc(sizeof(size)); printf(' Enter the elements in an array'); for(int i=0;i<size;i++) { scanf('%d',&p[i]); } return p; int main() *ptr; ptr="getarray();" length="sizeof(*ptr);" printf('elements that you have entered are : '); for(int i="0;ptr[i]!=' ';i++)" printf('%d ', ptr[i]); 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/16/return-an-array-c-6.webp" alt="Return an Array in C"> <p> <strong>Using Static Variable</strong> </p> <pre> #include int *getarray() { static int arr[7]; printf('Enter the elements in an array : '); for(int i=0;i<7;i++) { scanf('%d',&arr[i]); } return arr; int main() *ptr; ptr="getarray();" printf(' elements that you have entered are :'); for(int i="0;i<7;i++)" printf('%d ', ptr[i]); < pre> <p>In the above code, we have created the variable <strong>arr[]</strong> as static in <strong>getarray()</strong> function, which is available throughout the program. Therefore, the function getarray() returns the actual memory location of the variable ' <strong>arr</strong> '.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/16/return-an-array-c-7.webp" alt="Return an Array in C"> <p> <strong>Using Structure</strong> </p> The structure is a user-defined data type that can contain a collection of items of different types. Now, we will create a program that returns an array by using structure.<p></p> <pre> #include #include struct array { int arr[8]; }; struct array getarray() { struct array y; printf('Enter the elements in an array : '); for(int i=0;i<8;i++) { scanf('%d',&y.arr[i]); } return y; int main() struct array x="getarray();" printf('elements that you have entered are :'); for(int i="0;x.arr[i]!=' ';i++)" printf('%d ', x.arr[i]); 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/16/return-an-array-c-8.webp" alt="Return an Array in C"> <hr></8;i++)></pre></7;i++)></pre></size;i++)></pre></5;i++)></pre></5;i++)></pre></5;i++)></pre></5;i++)>7;i++)>5;i++)>5;i++)>5;i++)>5;i++)>