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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2D Array and double pointer

Status
Not open for further replies.

sedawk

Programmer
Feb 5, 2002
247
US
Hi,

This is a simple question:

I have a function accepts double pointer, the prototype is:
// code begin
void foo(int **m);
{
// do something with m
}
// code end

It works fine. The problem now is that in my main function the user only provide 2D array type data, e.g.,
// code begin
main()
{
a[3][3]= ... // an array;
foo(a); // <== warning and the program not working
}
// code end

I changed the line &quot;foo(a)&quot; to &quot;foo(&a)&quot; but not helpful. Still giving me the same warning:

'int ** ' differs in levels of indirection from 'int (*)[3][3]'

The compiler is VC 6++ on Windows XP. Now I have to transfer the input 2D array into double pointer array. I think there must be something can do this. I checked some posts here on passing 2D array to a function but not work in this case.

Thanks
 
> a[3][3]= ... // an array;
The prototype would be
void foo ( int a[3][3] );

And the call would be
foo ( a );

--
 
Hi Salem,

Thanks for your reply. I think I didn't describe the question clearly. Yes, I know the prototype:

void foo(int a[3][3]);

will work fine. But the problem is I can't change the prototype but only input data, ie., a[3][3] or **a in main() function. Now what I am doing is to converse all data form to double pointer array. It works fine.
 
Be careful about double-pointers. They could mean two different things:

1. A pointer to an array of pointers to arrays.
2. A pointer to a pointer to a flattened single-dimensional array.

When you define a[3][3] you are actually defining a flattened single-dimensional array, because that's how it's organized in memory. It does not make sense to use a double-pointer here, at least in your example.

A double-pointer would be more appropriate if you defined something like this which is a multi-level 2D-array:

typedef int row[3];
int row *a[3];

Here the array &quot;a&quot; contains three pointers to three objects of type &quot;row&quot;, which are themselves arrays of three integers each. Then it makes sense to pass &a which is a double-pointer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top