Article View: fr.comp.lang.c
Article #14993Re: Insérer une fonctions dans une structure?
From: Nicolas Cherel
Date: Tue, 18 Oct 2005 20:52
Date: Tue, 18 Oct 2005 20:52
47 lines
2684 bytes
2684 bytes
Pierre Maurette wrote:
> frederic@askme.fr, le 15/10/2005, a �crit :
>
>> Bonjour,
>>
>> y-a-t'il moyen en C, d'ins�rer une ou des fonctions dans une structure
>> de donn�es?
>>
>> typedef struct Complex Complex;
>> struct Complex {
>> int value;
>> int (*fn)();
>> char *mesg;
>> };
>
> Oui, c'est possible, c'est m�me facile. Vous aviez bien commenc�. Ce qui
> reste � faire, c'est initiliser et utiliser les membres fn de vos
> structures.
> Le plus simple pour initialiser C.fn, c'est de lui affecter l'adresse
> d'une fonction fonct. On peut pour cela utiliser indiff�remment fonct ou
> &fonct. Cette facilit� - que je trouve regrettable, mais c'est un troll
> - n'est pas unique dans le langage.
> Un code de test pourrait �tre (OK, le style est � chier):
>
> int carre(int i) {return i * i;}
> int cube(int i) {return i * i * i;}
>
> enum {ONE = 10, TWO};
>
> Complex table[] = {
> ONE, &cube, "premier message",
> TWO, &carre, "deuxi�me message",
> };
>
> printf("%d\t%d\t%s\n", table[0].value, table[0].fn(table[0].value),
> table[0].mesg);
> printf("%d\t%d\t%s\n", table[1].value, table[1].fn(table[1].value),
> table[1].mesg);
>
> Comme plusieurs fois signal�, soyez prudent avec les nommages. double
> doit �tre proscrit, et je n'aime pas trop Complex dans ce contexte.
>
En passant, pour les pointeurs de fonction : quelle est la diff�rence
entre "&fonction" et "fonction" ?. Un pointeur sur fonction peut �tre
allou� avec l'un ou l'autre, sans influence sur le r�sultat.
Si vous pouvez m'�clairer ...
Technical Details
Message-ID:
<43556096$0$7337$636a55ce@news.free.fr>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!infeed-4.proxad.net!nnrp6-1.free.fr!not-for-mailReferences:
<87r7amd6qx.fsf@venus.net> <mn.7ccf7d5a1b906ca4.31483@laposte.net>Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,d00d98e8032f6fc X-Google-Attributes: gidf9822,public X-Google-Language: FRENCH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!infeed-4.proxad.net!nnrp6-1.free.fr!not-for-mail Date: Tue, 18 Oct 2005 22:52:37 +0200 From: Nicolas Cherel <nicolas.cherel@free.fr> User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: fr.comp.lang.c Subject: Re: Insérer une fonctions dans une structure? References: <87r7amd6qx.fsf@venus.net> <mn.7ccf7d5a1b906ca4.31483@laposte.net> In-Reply-To: <mn.7ccf7d5a1b906ca4.31483@laposte.net> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8bit Lines: 48 Message-ID: <43556096$0$7337$636a55ce@news.free.fr> Organization: Guest of ProXad - France NNTP-Posting-Date: 18 Oct 2005 22:52:39 MEST NNTP-Posting-Host: 82.67.127.110 X-Trace: 1129668759 nnrp6-1.free.fr 7337 82.67.127.110:38972 X-Complaints-To: abuse@proxad.net