salut, j'ai ça qui marche:
je veux faire ça qui marche pas:Code:
1
2
3 #define M = 3 float w[2*M];
comment je fais?Code:
1
2
3
4 #define M = 3 int M2 = 2*M; float w[M2];
Version imprimable
salut, j'ai ça qui marche:
je veux faire ça qui marche pas:Code:
1
2
3 #define M = 3 float w[2*M];
comment je fais?Code:
1
2
3
4 #define M = 3 int M2 = 2*M; float w[M2];
PS : si ça marche pas c'est qu'il s'est pris les pieds dans ses lacets, regarde la fonction faireSesLacets() ;)Code:
1
2
3 #define M = 3 const int M2 = 2*M; float w[M2];
ça marche pas, ça se dit marcher!
ah non, par contre, ca cours...:mouarf::mouarf::mouarf: :mouarf3::mouarf3::mouarf2::mouarf2::mouarf1::mouarf1:Citation:
Envoyé par petdelascar
serieusement par contre, pense a mettre le tag :resolu:...
Qu'entends-tu par marcher?Citation:
Envoyé par petdelascar
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13 Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1 Copyright 1988-2003 Comeau Computing. All rights reserved. MODE:strict errors C99 "ComeauTest.c", line 2: error: expected an expression float w[2*M]; ^ "ComeauTest.c", line 2: error: this operator is not allowed in a constant expression float w[2*M]; ^ 2 errors detected in the compilation of "ComeauTest.c".[FONT=verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif]
J'entend ça à la compile:
qui correspond à:Code:
1
2
3error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0
float w[M2];
Bon, ba ça doit pas se faire, ça ça marche:
#define M_2 2 * M
Suis je bête, ça s'appelle l'allocation dynamique ça.
Plus précisément c'est une nouveauté du C99 : Les tableaux de taille variableCitation:
Envoyé par petdelascar
En admettant que tu veuilles direCitation:
Envoyé par petdelascar
2*M est une expression constante. OK.Code:#define M 3
2*M n'est pas une expression constante.Ca pourrait fonctionner en C99 (Variable Length Array ou VLA), mais je ne connais pas de compilateur qui implémente correctement les VLAs.Citation:
je veux faire ça qui marche pas:
Code:
1
2
3
4 #define M 3 int M2 = 2*M; float w[M2];
Comme d'habitude :Citation:
comment je fais?
avec contrôle et libération...Code:float *w = malloc (sizeof *w * M2);
Inclure <stdlib.h>