468,491 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

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 6756
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,207 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,207 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,207 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

Post your reply

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

Similar topics

5 posts views Thread by wilson | last post: by
2 posts views Thread by Ronnie Smith | last post: by
6 posts views Thread by Wijaya Edward | last post: by
9 posts views Thread by =?Utf-8?B?RGFya21hbg==?= | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.