473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to pass a 2-D array to a function

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]);

}
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 !
Aug 30 '05 #1
2 2344
Te-Jung Lo wrote:
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]);

}
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
Aug 30 '05 #2

"Te-Jung Lo" <49*******@s90.tku.edu.tw> ¼¶¼g©ó¶l¥ó·s»D:df**********@netnews.hinet.net...
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]);

}
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[i][j]);

}
Aug 30 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
6
by: Kenny | last post by:
Hello, can anyone tell me how to pass an array to a function ? I have this function , part of my class. It works if I do not put in int a everywhere , but obviously , I need to add an array so I...
1
by: Mark Dicken | last post by:
Hi All I have found the following Microsoft Technet 'Q' Article :- Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure (I've also copied and pasted the whole contents into...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
3
by: QQ | last post by:
I have one integer array int A; I need to pass this array into a function and evaluate this array in this function how should I pass? Is it fine? void test(int *a)
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.