Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pass a 2d array as a pointer

Status
Not open for further replies.

archit

Programmer
Aug 22, 2002
19
US
In the program below, the function "func1" operates on 2-D array of integers. The memory is allocated in the "main".

#define ROW (3)
#define COL (4)
#define ARRAY_SIZE (ROW*COL)

void func1(int matrix[][COL])
{
/* Matrix operation */
}

int main()
{

int * matrix = (int*)malloc(sizeof(int)*ARRAY_SIZE);

/* Call the array operation function func1 */
/* func1(----------------);*/

/* Do other computations */

free (matrix);
return 0;

}

Fill in the blank. (How do you call the func1?)

func1(------------------);

 
If you want a 2D array, change
Code:
int * matrix = (int*)malloc(sizeof(int)*ARRAY_SIZE);
to
Code:
int ** matrix = (int**)malloc(sizeof(int)*ARRAY_SIZE);
and change
[code]void func1(int matrix[][COL])
to
Code:
void func1(int **matrix)
//Daniel
 
Umm... Sorry about that post. Nevermind it. I must've left my brain at school or maybe at the bus. ;-)
This is how you would change it:
Code:
int * matrix = (int*)malloc(sizeof(int)*ARRAY_SIZE);
to
Code:
int **matrix = (int**)malloc(sizeof(int)*ROW);
int i;
for (i = 0; i < 3; i++)
{
	matrix[i] = (int*)malloc(sizeof(int)*COL);
}
and do the function change as stated above.
To call the function, just put
Code:
func1(matrix);
//Daniel
 
HI Denzil,
Thanks for the suggestion , but i belive apart from the function call nothing else can be changed ---this is the requirement . i can only fill change how i call the function if u read the question it is basically a fill in the blanks so i can just fill in func1(-------).I hope u got my problem now...????
 
Your program wouldn't even compile the way it is at the moment. Nor would you create a 2D array. To do what you want, you have to make those changes. //Daniel
 
Hi deniel,
I know that the program won't compile 'cause we have to fill the dashes in func1(-------------);

/* Call the array operation function func1 */
/* func1(----------------);*/

/* Do other computations */



DO U GET IT now ???


vikul
 
Do you not get what I'm saying or what? You have to make those changes for the program to compile. The void func1(int matrix[][COL]) is INVALID. And you said that you wanted a 2D array, why don't you want to create one? //Daniel
 
Sorry Daniel, I am not sure the construct
Code:
void func1(int matrix[][COL])
is invalid. In fact I am pretty sure it is valid.
You can omit the first size of a multi dimentionnal array in argument to function: the compiler does not need to know how many rows are in the array but it must know the size of each row to access the next one.

Depending on the way your compiler store multi dimentionnal arrays the following call could work.
Code:
func1((int (*)[COL])array)

I am not sure it is highly portable though.

It should work because multi dimentionnal arrays are stored internally as mono dimentionnal arrays. They are NOT stored as array of pointers to array. It could be true for other languages (like perl Arrays of Arrays) but not in C, at least in all the compilers I used till now. This would be highly inefficient.
 
Ok, I was under the impression that you had to put the number of rows in there. But having to put (int (*)[COL])array each time you want to access the array seems like a lot of hassle when you could just have a &quot;normal&quot; 2D array. //Daniel
 
i had seen a term from a material about the efficiency about using array. it told me that, &quot;eliminate the use of multidimentional array&quot;.

the reason is some compiler don't handle it well...

use single dimenttioanl array when possible.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top