logo

Devolver una matriz en C

¿Qué es una matriz?

Una matriz es un tipo de estructura de datos que almacena un tamaño fijo de una colección homogénea de datos. En resumen, podemos decir que una matriz es una colección de variables del mismo tipo.

cadena a carbón en java

Por ejemplo, si queremos declarar 'n' número de variables, n1, n2...n., si creamos todas estas variables individualmente, entonces se convierte en una tarea muy tediosa. En tal caso, creamos una matriz de variables del mismo tipo. Se puede acceder a cada elemento de una matriz utilizando un índice del elemento.

Primero veamos cómo pasar una matriz unidimensional a una función.

Pasar matriz a una función

 #include void getarray(int arr[]) { printf(&apos;Elements of array are : &apos;); 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(&apos;Elements of array are : &apos;); for(int i=0;i<5;i++) { printf('%c ', arr[i]); } int main() char arr[5]="{&apos;A&apos;,&apos;B&apos;,&apos;C&apos;,&apos;D&apos;,&apos;E&apos;};" 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(&apos;Enter the elements in an array : &apos;); 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&lt;5;i++)" printf('%d', n[i]); 0; < pre> <p>In the above program, <strong>getarray()</strong> function returns a variable &apos;arr&apos;. 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(&apos;Enter the elements in an array : &apos;); 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&lt;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(&apos;Enter the size of the array : &apos;); scanf(&apos;%d&apos;,&amp;size); int *p= malloc(sizeof(size)); printf(&apos;
Enter the elements in an array&apos;); 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]!=&apos;&apos;;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(&apos;Enter the elements in an array : &apos;); 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&lt;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 &apos; <strong>arr</strong> &apos;.</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(&apos;Enter the elements in an array : &apos;); 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]!=&apos;&apos;;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++)>