473,381 Members | 1,508 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,381 software developers and data experts.

How to pass 2D array to function

AmberJain
884 Expert 512MB
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 7061
gpraghuram
1,275 Expert 1GB
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 Expert 512MB
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 Expert 1GB
I was asking , did u write any code for this?


Raghuram
Feb 14 '08 #4
romcab
108 100+
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 Expert 512MB
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 Expert 512MB
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 Expert Mod 8TB
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 Expert 512MB
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
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 Expert Mod 8TB
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
My congratulations "weaknessforcats".
Functions are troubling me too and you made everything, more than crystal clear!
Feb 18 '08 #12
cjava
3
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 Expert Mod 8TB
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
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.