Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gobelair

passing multiple arrays in c++

This topic is 6008 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m making a program in c++. I''m trying to pass a multiple array from a function to another function and there save it in a other array. I''ve initialised it in the first function. But how can I pass the whole array to a other function. And there save it in a other variable and then entering the data in that other array? btw the size of the multiple array is variable. (Its a tile map)

Share this post


Link to post
Share on other sites
Advertisement
Simple answer
- You can't.

Short answer:
- You can't do it in the way you (obviously) seem to want to, but there are ways around it.

Longer answer
- If your sizes were fixed you could declare them in the type of the parameter : void foo( char array[10][10] );.
- You can pass it as a pointer, but will lose size information, so you will have to do the indexing calculation yourself, passing the size as parameter : array[column+rowsize*row].

Complex answer
- Build your muldimensional array as an array of arrays.
- Or use the standard C++ library's vector class which carries size information, making a vector of vector.
- Or grab Boost's multidimensional array class.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 30, 2002 9:30:24 AM]

Share this post


Link to post
Share on other sites
Ok thanks for repley but I''ve still got a problem.

How can I pass the array with a pointer? And how can I then acces the data? So how do I the indexing calculations?

Share this post


Link to post
Share on other sites
It sounds like your knowledge of C++ is a little shaky. I would recommend that you get a Teach Yourself C++ in 21 days book from your local library and refresh your memory. Thats what I just finished doing myself.

Once you create a class to store your matricies you will be able to pass them by value, pass a pointer by value, or pass by reference just like any other data type.

You can use the standard template library to create a vector or vectors. You can use a constructor to create a vector for each row, then add the rows to another vector, resulting in a vector of vectors. You can index a vector of vectors just like you would a 2 dimensional array.

This is what I am working on at the moment. A Matrix class that can handle all the matrix operations. I had to refresh my knowledge of C++ before undertaking this, if your C++ is rusty you might have to do the same. Or you can use one of the matrix libraries at

http://directory.google.com/Top/Computers/Programming/Languages/C%2B%2B/Class_Libraries/Numerics/

BTW Mathematica is an excellent learning tool if like me your matrix math is a little rusty. You can type in the matricies and multiply them and imediately see the results. Great for double checking you matrix functions.

Share this post


Link to post
Share on other sites
ok i''ve got a book about C++(a dutch book) and also programming role playing games with directX(Jim Adams). And I''ve decided to write my own (simple) 2D engine to learn how to do that.

In the book from Jim Adams he uses a method of passing a pointer from the multiple array. And then store it with the memcpy() function in a other pointer. later the data inside is used. Can some body explain my how that you can do that (or give a good link). I know it is a basic C++ question but I really can''t find a answere in the book i have and in the on-line tutorials about array''s i also can''t.

Share this post


Link to post
Share on other sites
Using memcpy might work, but its not good programming. It might be the best way for C, but its definately not the best way using C++.

Personally I would take the object oriented approach and create a matrix class, but if you are set on taking a functional programming approach then here is my suggestion.


  
//Define a Matrix data type using vectors

#include <vector>

#typedef vertor<int> Row; // dynamic array of integers

#typedef vector<Row> Matrix;// dynamic array of rows


//Initialize a Matrix

Matrix CreateMatrix(int numRows, int numColums)
{
Matrix m(numRows);
Row temp(numColumns;
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numColumns; j++)
{
temp[j] = 0;
}
m[i] = temp;
}
return m;
}
// I think this code is basically correct?


The STL takes care of the assignment operator,subscript operator, and copy constructors for you. So you are free to use =, [], pass by value ect.

Matrix a,b,c;

a = CreateMatrix(3,3); // a is no longer empty
b = a; // b is now a copy of a
a[0][0] = 5; // a is now different than b

The Standard Template Library is really worth the effort to learn. Don't let the syntax scare you. STL is a powerfull tool no C++ programmer should ignore.






[edited by - Raskolnikov on June 30, 2002 3:41:41 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by gobelair
Ok thanks for repley but I''ve still got a problem.

How can I pass the array with a pointer? And how can I then acces the data? So how do I the indexing calculations?


If you have an array int array[10][10], then the symbol array points to the first element. Then for index calculations, you just use array[‌i][j]≡array[j+row_size*i]

It should all be explained in the chapter on pointers and arrays of your C++ manual.

Good luck.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Or just contain the array and information in a class and pass the class and all the work is pretty much done for you, then just pull the information out of the class. Or a linked list will work even better, but that may just be a little more advanced than you are at this moment, but is well worth the effort and pays off immensely.



I know only that which I know, but I do not know what I know.

Share this post


Link to post
Share on other sites
ok i''ve still got a problem i''ve written this code but if I run the program only one tile is shown correctly. what i''m I doing wrong? btw I''ve used Jim Adams game core. the fist function is a create function second draw third render.


  
bool cIsoMap::Maken(cGraphics *pGraphics, cTexture *pTexture, int *pMap,int With, int Height, int Colom, int Row)
{
m_Graphics = pGraphics;
m_Texture = pTexture;

m_With = With;
m_Height = Height;

m_Colom = Colom;
m_Row = Row;

m_Map = new int[(m_Colom * m_Row)];
ZeroMemory(m_Map, (m_Row * m_Colom));

memcpy(&m_Map[0], pMap, m_Row);

m_Graphics->EnableAlphaTesting(true);

return true;
}

bool cIsoMap::Tekenen(int x, int y, int TexNum)
{
int xSrc = TexNum * m_With;
int ySrc = 0;

m_Texture->Blit(x, y, xSrc, ySrc, m_With, m_Height, 1.0f, 1.0f);

return true;
}

bool cIsoMap::Render(int xView, int yView)
{
int x, y, *ptr, TexNum;

ptr = &m_Map[0];

for(short i = 0; i < m_Row ; i++)
{
for(short j = 0; j < m_Colom; j++)
{
x = (i * 32) + (j * 32 );
y = (j * 16) - (i * 16 );

TexNum = ptr[(i * m_Row)+j];

Tekenen(x + xView, y + yView, TexNum);
}
}

return true;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by gobelair
I''m making a program in c++. I''m trying to pass a multiple array from a function to another function and there save it in a other array. I''ve initialised it in the first function. But how can I pass the whole array to a other function. And there save it in a other variable and then entering the data in that other array? btw the size of the multiple array is variable. (Its a tile map)




The easiest way to pass a multi dimensional array is as follows:

int array[3][3];
void function( int **ptrToArray );
function( array );

now if it were a 3 dimensional array it''d be
int array[3][3][3];
void function( int ***ptrToArray );
function( array )

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!