| | |
returning three dimensional array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
Hello
I am having no problem while returning 1 dimensinal array
But am having trouble with 3 dimenional array
No problem with this code
But this doesnt work
Is there anyway I can return 3 dimensional arrays ?
I am having no problem while returning 1 dimensinal array
But am having trouble with 3 dimenional array
No problem with this code
C Syntax (Toggle Plain Text)
float Mat[][][]; float Mat[]; float *funct1(void) { return Mat; } void funct2() { float *Mat; Mat=funct1(); }
But this doesnt work
C Syntax (Toggle Plain Text)
float Mat[][][]; float *funct1(void) { Mat[][][]={........}; return Mat; } void funct2() { float *Mat; Mat=funct1(); }
Is there anyway I can return 3 dimensional arrays ?
•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Solved Threads: 0
For example
C Syntax (Toggle Plain Text)
multi_return.c: In function ‘funct1’: multi_return.c:27: warning: return from incompatible pointer type multi_return.c:27: warning: function returns address of local variable
C Syntax (Toggle Plain Text)
#include <stdio.h> float Mat[4][4][4]; float *funct1(void) { float Mat[4][4][4]={ 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5,}; return Mat; } void funct2() { int i,j,k; float *Mat; Mat=funct1(); for(k=0; k<4;k++) { for (i=0; i<4; i++) { for (j=0; j<4; j++) { // printf(" %f ", Mat[k][i][j]); } printf("\n"); } printf("\n"); } } int main() { funct2(); }
I'm sure you are getting a perfectly fine return value. Your problem is that you have not supplied any type information that the compiler can use to "know" about your array.
First, please see the Wikipedia here.
Next, please be aware that only the first dimension can be [] in C, and only when you are not actually creating data. Other dimensions must have a size. So
is an error, as is
but
is not.
However,
If your array has variable size dimensions, then you should use a simple flat array (or pointer) and calculate things on your own:
If your Mat object is a variable-sized array, you should create a small library of functions to handle it, and place all relevant information in a structure. For example:
Hope this helps.
First, please see the Wikipedia here.
Next, please be aware that only the first dimension can be [] in C, and only when you are not actually creating data. Other dimensions must have a size. So
int Mat[][][];is an error, as is
int Mat [][10][5];but
void printarray( int a[][10][5], int depth );is not.
However,
int *Mat; knows nothing about how many dimensions the array has, or even if it really is an array.If your array has variable size dimensions, then you should use a simple flat array (or pointer) and calculate things on your own:
C Syntax (Toggle Plain Text)
int *index_2d_array( int *arr, int height, int width, int y, int x ) { int index = (y * width) + x; return &(arr[ index ]); } // two differently sized arrays with a "center" element int a[5][5] = { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 7, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }; int b[3][5] = { { 0, 0, 0, 0, 0 }, { 0, 0, 4, 0, 0 }, { 0, 0, 0, 0, 0 } } // a[3][4] = b[1][3] *index_2d_array( a, 5, 5, 3, 4 ) = *index_2d_array( b, 3, 5, 1, 3 );
If your Mat object is a variable-sized array, you should create a small library of functions to handle it, and place all relevant information in a structure. For example:
C Syntax (Toggle Plain Text)
typedef struct { int *data; int width, height, depth; } Mat_t; int *Mat_element( Mat_t Mat, int z, int y, int x );
Hope this helps.
•
•
Join Date: Oct 2007
Posts: 41
Reputation:
Solved Threads: 0
thanks
I think this one works fine
I think this one works fine
C Syntax (Toggle Plain Text)
#include <stdio.h> float *array(void); float *array(void) { static float Mat[4][4][4]={ 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5, 1.2,1.3,1.4,1.5,}; return (float *) Mat; } int main(void) { float *Mat, i; Mat = array(); for (i = 0; i < 16; i++) { printf("\n %f", *(Mat++)); } return 0; }
![]() |
Similar Threads
- Need Help With two-dimensional array (VB.NET)
- Safe Array (C++)
- Returning 2-dimensional array, how? (C)
- two dimensional array (C)
- multi dimensional array search xml parser (PHP)
- Need help passing a multi-dimensional array (C++)
- Trying to create a method to convert string letters into a two dimensional array (Java)
Other Threads in the C Forum
- Previous Thread: Need help in a very easy C progrem
- Next Thread: Copy argv to string in C(newbie question)
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char character convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest intmain() kilometer license linked linkedlist linux linuxsegmentationfault list logical_drives match matrix meter microsoft motherboard multi mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions test testautomation unix urboc user voidmain() win32api windows.h






