473,505 Members | 13,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass 2D array to function

AmberJain
884 Recognized Expert Contributor
Well, I got this question as an assignment-------->

Write a C program to pass a 2D array (of characters and integers both) to a function?

Thanks in advance to everyone...................
ambrnewlearner
Feb 12 '08 #1
13 7064
gpraghuram
1,275 Recognized Expert Top Contributor
Well, I got this question as an assignment-------->

Write a C program to pass a 2D array (of characters and integers both) to a function?

Thanks in advance to everyone...................
ambrnewlearner

When passing a 2 d array to a function then you should pass the array as well as the size of the array.
But what did u try anything on this
Raghuram
Feb 12 '08 #2
AmberJain
884 Recognized Expert Contributor
When passing a 2 d array to a function then you should pass the array as well as the size of the array.

???????????? But what did u try anything on this

Raghuram
__________________________________________________ _____
What do you mean by : "But what did u try anything on this"

ambrnewlearner
__________________________________________________ _____
Feb 13 '08 #3
gpraghuram
1,275 Recognized Expert Top Contributor
I was asking , did u write any code for this?


Raghuram
Feb 14 '08 #4
romcab
108 New Member
You need the index in passing 2D array. You can refer to this simple code.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. void Accept(int arr[][2], int row, int col);
  4.  
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7.     int myArray[3][2] = { {0,0}, {1,1}, {2,2} };
  8.     Accept(myArray, 3, 2);
  9.     return 0;
  10. }
  11.  
  12. void Accept(int arr[][2], int row, int col )
  13. {
  14.     int i, j;
  15.     for ( i = 0; i < row; i++ )
  16.         for ( j = 0; j < col; j++ )
  17.             arr[i][j] = 5;
  18. }
Feb 14 '08 #5
AmberJain
884 Recognized Expert Contributor
I was asking , did u write any code for this?


Raghuram
__________________________________________________ __________
Well, [quite foolishly] my answer is NO. Actually I am still learning functions in C and don't have a good command over functions. I missed some of my lectures at college [I was ill] and therefore don't know much about functions and later on my faculty couldnot explain it to me perfectly and so I posted this question on thescripts.
HOPE that someone explains me the complete concept of this question

__________________________________________________ ___________
Feb 14 '08 #6
AmberJain
884 Recognized Expert Contributor
You need the index in passing 2D array. You can refer to this simple code.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. void Accept(int arr[][2], int row, int col);
  4.  
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7.     int myArray[3][2] = { {0,0}, {1,1}, {2,2} };
  8.     Accept(myArray, 3, 2);
  9.     return 0;
  10. }
  11.  
  12. void Accept(int arr[][2], int row, int col )
  13. {
  14.     int i, j;
  15.     for ( i = 0; i < row; i++ )
  16.         for ( j = 0; j < col; j++ )
  17.             arr[i][j] = 5;
  18. }
__________________________________________________ ______

<---------newbie here--------->
Can you please explain it in more detail
__________________________________________________ ______
Feb 14 '08 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
Please read this:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
In each case value is the number of elements. Any other brackets only describe the elements.

Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

With the above in mind this array:
Expand|Select|Wrap|Line Numbers
  1. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Wheras this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Kinda the same, right?

So if your disc file contains

0 1 2 3 4 5 6 7 8 9

Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
and then post again if you are still stuck.
Feb 14 '08 #8
AmberJain
884 Recognized Expert Contributor
Please read this:

YOUR REPLY

and then post again if you are still stuck.
__________________________________________________ _______
THANKS weaknessforcats........................
This is certainly one of best and brief tutorial on arrays. But I'm stuck with functions and not arrays in my question "WRITE A PROGRAM TO PASS A 2D ARRAY TO A FUNCTION". I missed my lecture on functions and therefore if someone can guide me on functions or recommend a good book as a "self tutorial" for functions, then probably my problem will be solved....
Feb 16 '08 #9
Simonius
47 New Member
Code: ( c )
1. int array[];
won't compile. You need to declare the number of elements.
It'll compile if you fill it in like int array[]={0,1,2,3,4,5,6,7,8,9};
Feb 16 '08 #10
weaknessforcats
9,208 Recognized Expert Moderator Expert
It'll compile if you fill it in like int array[]={0,1,2,3,4,5,6,7,8,9};
Of course it will, you specified the number of elements. This is initialization syntax only. The compiler will create an array for that number of elements and intialize each element to its respective value.

But I'm stuck with functions and not arrays in my question "WRITE A PROGRAM TO PASS A 2D ARRAY TO A FUNCTION". I
Read that tutorial again and tattoo on your head: The name of the array is the address of element 0.

This array:
Expand|Select|Wrap|Line Numbers
  1. int arr[3][4][5];
  2.  
can be passed to a function:
Expand|Select|Wrap|Line Numbers
  1. func(arr);
  2.  
if the argument to func() is a pointer to a [4][5] array.

But if the func argiument is a pointer to an [5] array of int, you need to pass the &arr[0][0].

But if the func argiument is a pointer to an int, you need to pass the &arr[0][0][0].

As you omit dimensions you need to add arguments to funct() since the "array-ness" is lost on the call. This is called decay of array and this occurs whenever an array is pass to a function. From inside the function all you see is an address and not an array.
Feb 17 '08 #11
cube
18 New Member
My congratulations "weaknessforcats".
Functions are troubling me too and you made everything, more than crystal clear!
Feb 18 '08 #12
cjava
3 New Member
Of course it will, you specified the number of elements. This is initialization syntax only. The compiler will create an array for that number of elements and intialize each element to its respective value.



Read that tutorial again and tattoo on your head: The name of the array is the address of element 0.

This array:
Expand|Select|Wrap|Line Numbers
  1. int arr[3][4][5];
  2.  
can be passed to a function:
Expand|Select|Wrap|Line Numbers
  1. func(arr);
  2.  
if the argument to func() is a pointer to a [4][5] array.

.
Cool ..

Let me make some other points clear ..

The problem is to pass a 2 dimensional array to a function.

From your question I presume you want to pass the array by reference because only that would make sense.

First of all in order to do that you need to understand certain nuances for passing an array.

when you declare

int a [5]

then an memory of 5 contiguous spaces is first allocated and given an integer view.

Then this integer memory is assigned to a.

hence typing simply a refers to the first location of the memory.

*a would be equivalent to a[0].

However when it comes to Multidimensional arrays C does not have great support for them.

They are usually visualized as array of arrays.

Expand|Select|Wrap|Line Numbers
  1. for ex. 
  2.  
  3.  [ ] -> [Row 1]
  4.  [ ] -> [Row 2]
  5.  [ ] -> [Row 3]
  6.  
now..

there are two ways to declare an 2D array :
Expand|Select|Wrap|Line Numbers
  1. a) int a[rows] [cols]
  2. b )Simulated using double pointers.
So its important to know how you 2D array is declared.

Given all these Here are the ways to pass it to the functions :

let fn be the function receiving the 2D array.

Then

Expand|Select|Wrap|Line Numbers
  1. int fn (int a[][cols]);
  2. int fn ( int a[rows][cols]);
  3. int fn(int *ptr,int rows,int cols) 
  4. int fn (int **ptr ) (Only if you declared using Simulated methd)
  5.  
Some word of caution

Expand|Select|Wrap|Line Numbers
  1. int a[rows][cols]
a = *a = adrress of the first element of the first row.
so never try to use **a if you use the above method to declare 2D arrays.
you can use int *ptr = a to pass the array address.
Two dimensional array is not double pointer.
Feb 18 '08 #13
weaknessforcats
9,208 Recognized Expert Moderator Expert
There is now an article about this in the C/C++ HowTos forum.

http://www.thescripts.com/forum/thread772412.html.
Feb 19 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
31529
by: John | last post by:
I would like to pass array variables between URLs but I don't know how. I know its possible with sessions, but sessions can become useless if cookies are disabled. I have unsuccessfully tried...
2
5196
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
5
3403
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? ...
2
2484
by: Ronnie Smith | last post by:
I am trying to pass a function (method) address to a user control to associate with an event. The small user control which sends events is contained in a larger user control group which is in...
4
146668
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
6
3494
by: Wijaya Edward | last post by:
Hi, How can I pass Array, Hash, and a plain variable in to a function at the same time. I come from Perl. Where as you probably know it is done like this: sub myfunc {
0
1927
by: Jagdish | last post by:
Hello, Every body I have recently joined this group and I want to know how to pass Array of string to a Com object function.. Actually I want to pass String array from VB6 to Com Interface...
8
2306
by: laredotornado | last post by:
Hi, I want to pass my function myFunc('a', 'b', 'c') as an argument to another function. However, this will not work doStuff('x', 'y', myFunc('a', 'b', 'c'))
9
2670
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
0
7213
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
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
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,...
1
7017
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
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.