int f(const int tab[2][1])
{
return 0;
}
int g(void)
{
int tab[2][1];
return f(tab);
}
En compilant ce fichier, j'obtiens :
% gcc t.c -Wall -c -W -ansi -pedantic
t.c: In function `f':
t.c:3: warning: unused parameter `tab'
t.c: In function `g':
t.c:12: warning: passing arg 1 of `f' from incompatible pointer type
Je comprends parfaitement le premier avertissement, mais pas le deuxieme.
Pouvez-vous m'eclairer ?
Patrick
Pour moi, et sous réserve que le code soit un copié/collé du tien, ce warning
n'est pas justifié.
Pour PCLint, non plus :
D:\CLC\P\Pelissie>\splint~1.6\bin\splint.exe -f \clc\borland.rc main.c
Splint 3.0.1.6 --- 11 Feb 2002
main.c(1,18): Function parameter tab declared as manifest array (size
constant is meaningless)
A formal parameter is declared as an array with size. The size of the array
is ignored in this context, since the array formal parameter is treated as a
pointer. (Use -fixedformalarray to inhibit warning)
Finished checking --- 1 code warning
--
-ed- emdelY...@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Le paramčtre a le type
const int (*) [1]
(pointeur vers tableau d'un const int)
et l'argument a le type
int (*) [1]
(pointeur vers tableau d'un int)
Selon la norme il n'y a pas de conversion automatique entre ces types.
Il faut un cast
return f ( (const int(*)[1])tab );
--
Horst
>> int f(const int tab[2][1])
>> {
>> return 0;
>> }
>>
>> int g(void)
>> {
>> int tab[2][1];
>>
>> return f(tab);
>> }
>>
>> t.c: In function `g':
>> t.c:12: warning: passing arg 1 of `f' from incompatible pointer type
>>
> Le paramètre a le type
>
> const int (*) [1]
>
> (pointeur vers tableau d'un const int)
>
>
> et l'argument a le type
>
> int (*) [1]
>
> (pointeur vers tableau d'un int)
>
>
> Selon la norme il n'y a pas de conversion automatique entre ces types.
> Il faut un cast
J'aurais dit le contraire... Mais je ne t'ai jamais vu commetre d'erreur ici,
alors je suis dubitatif...
> return f ( (const int(*)[1])tab );
Pour moi, un pointeur sur const peut recevoir la valeur d'un pointeur. C'est
à cause des 2 dimensions que ça pose problème?
> > return f ( (const int(*)[1])tab );
> Pour moi, un pointeur sur const peut recevoir la valeur d'un
> pointeur. C'est à cause des 2 dimensions que ça pose problème?
Oui, apparemment. Avec une dimension, aucun problème.
--
Vincent Lefèvre <vin...@vinc17.org> - Web: <http://www.vinc17.org/> - 100%
validated (X)HTML - Acorn Risc PC, Yellow Pig 17, Championnat International
des Jeux Mathématiques et Logiques, TETRHEX, etc.
Work: CR INRIA - computer arithmetic / SPACES project at LORIA
>In 'fr.comp.lang.c', Horst Kraemer <horst....@epost.de> wrote:
>
>>> int f(const int tab[2][1])
>>> {
>>> return 0;
>>> }
>>>
>>> int g(void)
>>> {
>>> int tab[2][1];
>>>
>>> return f(tab);
>>> }
>>>
>>> t.c: In function `g':
>>> t.c:12: warning: passing arg 1 of `f' from incompatible pointer type
>>>
>> Le paramètre a le type
>>
>> const int (*) [1]
>>
>> (pointeur vers tableau d'un const int)
>>
>>
>> et l'argument a le type
>>
>> int (*) [1]
>>
>> (pointeur vers tableau d'un int)
>>
>>
>> Selon la norme il n'y a pas de conversion automatique entre ces types.
>> Il faut un cast
>
>J'aurais dit le contraire... Mais je ne t'ai jamais vu commetre d'erreur ici,
>alors je suis dubitatif...
>
>> return f ( (const int(*)[1])tab );
>
>Pour moi, un pointeur sur const peut recevoir la valeur d'un pointeur. C'est
>à cause des 2 dimensions que ça pose problème?
La norme dit qu'il y a une conversion automatique
toto * p
vers
const|volatile toto * p
cela ne s'étend ni à (1)
toto ** p
vers
const|volatile toto ** p
et ni à (2)
toto (*p) [N]
vers
const|volatile toto (*p)[N]
On peut montrer que (1) ne serait pas sûre,
(
const char cc[] = "hello";
void f( const char **cpp)
{
*cpp = cc; /* OK */
}
char *p;
f(&p); /* ??? */
p[0] = 'H'; /* comportement indéfini, f a fait pointer
p vers char const */
)
mais apparamment (2) serait sûre mais la norme y tient - peut-etre
pour des raisons formelles.
--
Horst