Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a 2D array from a function in C?

Tags:

arrays

c

return

I am a Ruby programmer who has ended up developing a code generate for C. Its like asking a Limo to tow a 1960s truck. Any way.

Here is what I thought should work but doesnt work.

float[][] pixels() {   float x[][]= { {1,1},{2,2} };   return x }  void drawLine(float x[][2]) {   //drawing the line }  //inside main drawLine(pixels()); 

I have banged my head on my desk trying to get this thing work. Please help.

like image 762
Eastern Monk Avatar asked Mar 05 '11 04:03

Eastern Monk


People also ask

How do I return a 2D array?

Use Pointer to Pointer Notation to Return 2D Array From Function in C++ As an alternative, we can use a pointer to pointer notation to return the array from the function. This method has an advantage over others if the objects to be returned are allocated dynamically.

Can you return an array in C from a function?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

Can a function return a matrix in C?

You can "upgrade" the function by return the matrix like this: int *mat(int x, int y, const int *m_a, const int *m_b, int *m_out){ // do some work if (something is not OK) return NULL; else return m_out; } // in main() int a[5 * 6]; int b[5 * 6]; int result[5 * 6]; mat(5, 6, a, b, result); // use result.

How do you return a 2D array in C++?

Use Pointer Notation to Return 2D Array From Function in C++ Return by the pointer is the preferred method for larger objects rather than returning them by value. Since the 2D array can get quite big, it’s best to pass the pointer to the first element of the matrix, as demonstrated in the following code example.

How to return a variable from an array in C++?

In C/C++, when you pass an array to a function, it decays to be a pointer pointing to first element of the array. So, in pixels () function, you are returning the address of a stack allocated variable. The returning variable's address is no longer valid because on pixels () return, the stack allocated variable goes out of scope.

How to return a pointer to an array from a function?

However, you can return a pointer to an array by specifying the array's name without an index. If you want to return a single-dimension array from a function, you would have to declare a function returning a pointer as in the following example −

How do you pass a 2-D array to a function?

We have learned that in chapter Two Dimensional Array in C that when a 2-D is passed to a function it is optional to specify the size of the left most dimensions. So if we have an array of 2 rows and 3 dimensions then it can be passed to a function in the following two ways: 1 2 3 4 int two_d = { {99,44,11}, {4,66,9} };


1 Answers

In C, pointers and arrays are closely related. Also, you usually need to pass the size of an array as a separate variable. Let's start you with:

#include <stdio.h>  float** createArray(int m, int n) {     float* values = calloc(m*n, sizeof(float));     float** rows = malloc(m*sizeof(float*));     for (int i=0; i<m; ++i)     {         rows[i] = values + i*n;     }     return rows; }  void destroyArray(float** arr) {     free(*arr);     free(arr); }  void drawLine(const float** coords, int m, int n);  int main(void) {     float** arr = createArray(2,2);     arr[0][0] = 1;     arr[0][1] = 1;     arr[1][0] = 2;     arr[1][1] = 2;     drawLine(arr, 2, 2);      destroyArray(arr); } 
like image 69
aschepler Avatar answered Sep 26 '22 00:09

aschepler