Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,684 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,496 people online right now. Registration is fast and FREE... Join Now!




Passing A 2D Array to a function..

 
Reply to this topicStart new topic

Passing A 2D Array to a function..

chrisfields
post 10 Oct, 2006 - 05:32 PM
Post #1


New D.I.C Head

*
Joined: 20 Sep, 2006
Posts: 12


My Contributions


CODE

void populateSquareMatrix(int[],int);//functionp prototype//

int SquareMatrix[ArraySize][2];//2D array//

when i try to pass the array shown above to the function populateSquareMatrix i get a syntax error:
QUOTE

>>: error C2664: 'populateSquareMatrix' : cannot convert parameter 1 from 'int [20][2]' to 'int []'

if i include a second set of [] to the prototype that causes even more errors how would i pass a 2d array to the function?

this is how i call how i call the function.

populateSquareMatrix(SquareMatrix,ArraySize);
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 10 Oct, 2006 - 06:20 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


http://www.cplusplus.com/doc/tutorial/arrays.html

When passing a two dimensional array to a function in that manner, C++ expects to know the number of elements in the second dimension, like so
CODE

//call the function
myArrayFunc(myarr[][10]);

There are workarounds however...you can use vectors, or even pass the array as a pointer.
User is online!Profile CardPM

Go to the top of the page

chrisfields
post 10 Oct, 2006 - 06:45 PM
Post #3


New D.I.C Head

*
Joined: 20 Sep, 2006
Posts: 12


My Contributions


populateSquareMatrix(SquareMatrix[][2],ArraySize);

gives error:
QUOTE
'
User is offlineProfile CardPM

Go to the top of the page

gregoryH
post 11 Oct, 2006 - 12:51 AM
Post #4


D.I.C Regular

Group Icon
Joined: 4 Oct, 2006
Posts: 417



Dream Kudos: 50
My Contributions


QUOTE(chrisfields @ 10 Oct, 2006 - 07:45 PM) *

populateSquareMatrix(SquareMatrix[][2],ArraySize);

gives>>c:\error C2059: syntax error : ']'


how did you set up the signature of the function?

the alternate is to pass as pointer to pointer ( TYPE ** variable ), but you need a little more house keeping data to go with that.
User is offlineProfile CardPM

Go to the top of the page

chrisfields
post 11 Oct, 2006 - 10:38 AM
Post #5


New D.I.C Head

*
Joined: 20 Sep, 2006
Posts: 12


My Contributions


CODE

//function definition//
void populateSquareMatrix(int array[][2],int size)
{
statements.....
}


this way worked i also had to include it in the function definition itself
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 11 Oct, 2006 - 03:09 PM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


This holds true for any function...the protottype much match the definition which must match the use.
User is online!Profile CardPM

Go to the top of the page

chrisfields
post 12 Oct, 2006 - 08:24 AM
Post #7


New D.I.C Head

*
Joined: 20 Sep, 2006
Posts: 12


My Contributions


QUOTE(Amadeus @ 11 Oct, 2006 - 04:09 PM) *

This holds true for any function...the protottype much match the definition which must match the use.


CODE
void populateSquareMatrix(int array[ArraySize][2],int size)
{
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            printf("Row %d, Col %d: ",i,j);
            SquareMatrix[i][j]=(rand()%10);
            //SquareMatrix[][j](rand()%10);
            printf("%d,%d ",SquareMatrix[i][j]);
            printf("\n");
        }
    }



i am trying to store in 20 rows.... two numers in the 2 columns for each row
eg. row[0]=12,23
row[1]=34,56 etc etc

This post has been edited by Dark_Nexus: 31 Oct, 2006 - 10:14 PM
User is offlineProfile CardPM

Go to the top of the page

amitgtbit
post 13 Oct, 2006 - 12:57 AM
Post #8


New D.I.C Head

*
Joined: 19 Jan, 2006
Posts: 2


My Contributions


Just pass the array as function_name(arr[][],int);

and reciverd it as the pointer and than just perform the normal poniter operations as an array you will get all the answers their and if u wan a confirm than use book named pointers in C by yashwant Kanitkar.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 13 Oct, 2006 - 09:52 AM
Post #9


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


QUOTE
Just pass the array as function_name(arr[][],int);


You need to specify the number of columns for the 2D Array.
Many people make this mistake.

To know why C/C++ requires us to do this, Click Here to visit my Blog for the answer.
User is offlineProfile CardPM

Go to the top of the page

chrisfields
post 14 Oct, 2006 - 12:40 PM
Post #10


New D.I.C Head

*
Joined: 20 Sep, 2006
Posts: 12


My Contributions


CODE

//function definition//
void populateSquareMatrix(int array[][2],int size)
{
    for(int row = 0; row< size; row++)
    {
        
        for(int col= 0; col< 2; col++)
        {
            SquareMatrix[row][col]=(rand()%10);
            printf("Row:%d\tCol:%d = %d ",row,col,SquareMatrix[row][col]);
            printf("\n");
        }
        
    }

}

yup it worked this way.....now here comes te hard part
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 14 Oct, 2006 - 10:39 PM
Post #11


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


QUOTE
//function definition//

I hope you know that // considers the entire line as a comment and cannot be closed by using // again (unlike /* ... */ )


QUOTE
now here comes te hard part

Which part are to referring to now?
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 07:55AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month