C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passage de matrice en parametre d'une foction

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
J?r?me
Guest





PostPosted: Mon May 30, 2005 8:58 pm    Post subject: Passage de matrice en parametre d'une foction Reply with quote



Salut!

Je voudrais passer une matrice carrée de taille variable à une
fonction. Comment dois-je m'y prendre?

ex

f(int **mat, int dim)
{
blah blah
}
int main()
{
int mat[6][6];
f(mat,6);
}

le problème c'est que g++ ne reconnait pas ma matrice comme un int
**?!
[email]je.c (AT) laposte (DOT) net[/email]

Si vous avez une idée?
Back to top
Falk Tannhäuser
Guest





PostPosted: Mon May 30, 2005 9:36 pm    Post subject: Re: Passage de matrice en parametre d'une foction Reply with quote



J?r?me wrote:

Quote:
Salut!

Je voudrais passer une matrice carrée de taille variable à une
fonction. Comment dois-je m'y prendre?

template<typename T, unsigned M, unsigned N>
void f(T (&mat)[M][N])
{
// ...
}

int main()
{
int mat[6][6];
f(mat);
return 0;
}

Sinon il vaut peut-être mieux écrire une classe "Matrix".

Falk

Back to top
Jérôme
Guest





PostPosted: Mon May 30, 2005 10:36 pm    Post subject: Re: Passage de matrice en parametre d'une foction Reply with quote



OK merci beaucoup j'ai créé un classe matrice comme tu me l'as
conseillé c'est encore plus simple!

Back to top
Horst Kraemer
Guest





PostPosted: Mon May 30, 2005 10:47 pm    Post subject: Re: Passage de matrice en parametre d'une foction Reply with quote

[email]partirensemble (AT) gmail (DOT) com[/email] (J?r?me) wrote:

Quote:
Salut!

Je voudrais passer une matrice carrée de taille variable à une
fonction. Comment dois-je m'y prendre?

ex

f(int **mat, int dim)
{
blah blah
}
int main()
{
int mat[6][6];
f(mat,6);
}

le problème c'est que g++ ne reconnait pas ma matrice comme un int
**?!

Les types int ** et int [n][n] (int (*) [n]) n'ont aucune relation
entre eux.


Solution laide

void f(int *mat, int dim)
{
/*
au lieu de mat[i][j] il faut écrire
mat[i*dim+j]
*/
}

f((int*)mat,6);

--
Horst

--
Lâche pas la patate!

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.