how to pass a 2-D array to a function

T

Te-Jung Lo

the following is the original code i wrote

#include <stdio.h>
#include <stdlib.h>


//--- Prototype ---//
void bfs(int []);

void main()
{
int mess[64] = { 0, 1, 1, 0, 0, 0, 0, 0,
1, 0, 0, 1, 1, 0, 0, 0,
1, 0, 0, 0, 0, 1, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 0, 1, 1, 1, 1, 0 };
bfs(mess); // Breadth First Search

}

void bfs(int mess[8][8])
{
for(int i=0; i < 64; i++)
printf("%d ", mess);

}


i've tried to pass a 2-D array to function with VC++ 6.0 compiler. There is
all right in compiler process, but two errors in link process as follows:

-----------------------------------------------------------------
Linking...
test.obj : error LNK2001: unresolved external symbol "void __cdecl bfs(int *
const)" (?bfs@@YAXQAH@Z)
Debug/test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

test.exe - 2 error(s), 0 warning(s)
-------------------------------------------------------------------

i know 2-D array is actually a line of memory space, so it would be correct
if change mess[8][8] into mess[64]
But is there another declaration can direct attain the goal(pass 2-D array
into a function)

thanks !
 
V

Victor Bazarov

Te-Jung Lo said:
the following is the original code i wrote

#include <stdio.h>
#include <stdlib.h>


//--- Prototype ---//
void bfs(int []);

void main()

int main()
{
int mess[64] = { 0, 1, 1, 0, 0, 0, 0, 0,
1, 0, 0, 1, 1, 0, 0, 0,
1, 0, 0, 0, 0, 1, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 0, 1, 1, 1, 1, 0 };
bfs(mess); // Breadth First Search

}

void bfs(int mess[8][8])

This is an overloaded 'bfs' function. The one you declared above 'main'
has a different argument: a pointer to int. This 'bfs' has the argument
of type 'an array of 8 arrays of int'.
{
for(int i=0; i < 64; i++)
printf("%d ", mess);

}


i've tried to pass a 2-D array to function with VC++ 6.0 compiler. There is
all right in compiler process, but two errors in link process as follows:

-----------------------------------------------------------------
Linking...
test.obj : error LNK2001: unresolved external symbol "void __cdecl bfs(int *
const)" (?bfs@@YAXQAH@Z)
Debug/test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

test.exe - 2 error(s), 0 warning(s)
-------------------------------------------------------------------

i know 2-D array is actually a line of memory space, so it would be correct
if change mess[8][8] into mess[64]
But is there another declaration can direct attain the goal(pass 2-D array
into a function)


You just need to (a) make both the "prototype" and the function have the
same declaration and (b) make sure you're actually passing a two-dim array
to the function. In the code you _declare_ a function 'bfs' to take what
is essentially a *one*-dimensional array, then declare a one-dimensional
array and pass it, but in the function _implementation_ you attempt to
accept the argument *as if* it is a two-dimensional array. You can't
expect this to work. Once a one-dimensional array, always
a one-dimensional array. IOW, make up your mind about the number of the
dimensions you intend to use.

V
 
T

Te-Jung Lo

Te-Jung Lo said:
the following is the original code i wrote

#include <stdio.h>
#include <stdlib.h>


//--- Prototype ---//
void bfs(int []);

void main()
{
int mess[64] = { 0, 1, 1, 0, 0, 0, 0, 0,
1, 0, 0, 1, 1, 0, 0, 0,
1, 0, 0, 0, 0, 1, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 0, 1, 1, 1, 1, 0 };
bfs(mess); // Breadth First Search

}

void bfs(int mess[8][8])
{
for(int i=0; i < 64; i++)
printf("%d ", mess);

}


i've tried to pass a 2-D array to function with VC++ 6.0 compiler. There
is all right in compiler process, but two errors in link process as
follows:

-----------------------------------------------------------------
Linking...
test.obj : error LNK2001: unresolved external symbol "void __cdecl bfs(int
* const)" (?bfs@@YAXQAH@Z)
Debug/test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

test.exe - 2 error(s), 0 warning(s)
-------------------------------------------------------------------

i know 2-D array is actually a line of memory space, so it would be
correct if change mess[8][8] into mess[64]
But is there another declaration can direct attain the goal(pass 2-D array
into a function)

thanks !


thank you so much
i take your recommendation (b) to modify the ori code, and it passes both
compiler & link process

the following is the modified code:

#include <stdio.h>
#include <stdlib.h>


//--- Prototype ---//
void bfs(int [8][8]);

void main()
{
int mess[8][8] = { 0, 1, 1, 0, 0, 0, 0, 0,
1, 0, 0, 1, 1, 0, 0, 0,
1, 0, 0, 0, 0, 1, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 0, 1, 1, 1, 1, 0 };
bfs(mess); // Breadth First Search

}


void bfs(int mess[8][8])
{
for(int i=0; i < 8; i++)
for(int j=0; j < 8; j++)
printf("%d ", mess[j]);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top