int GetNb(char c, char *str) { for (int i=0; ;str++,i++) (*str ? ((*str == c) ? return (i) : continue ) : return (-1)); return -1; }
gcc 3.2 special mingw
il me dit que c un erreur lors d'un (des) return
int GetNb(char c, char *str) { for (int i=0; ;str++,i++) (*str ? ((*str == c) ? return (i) : continue ) : return (-1)); return -1; }
int GetNb(char c, char *str) { for (int i=0; ;str++,i++) (*str ? ((*str == c) ? ({return (i);}) : ({continue;}) ) : ({return (-1);})); return -1; }
int GetNb(char c, char *str) { for (int i=0; ;str++,i++) return (*str ? ((*str == c) ? i : ({continue; 0})) : -1); return -1; }
nEUrOO
: ce code c'était pour optimiser et m'amuser aussi ...
Sally :
Et au fait, si ({continue}) est une expression, quels sont son type et sa valeur ?
nitro
:nEUrOOComme je l'ai dit, généralement tout mettre dans une seule instruction n'optimise rien du tout, et quelque fois ça pessimise, comparé à du code trivial que le compilateur peut manipuler plus librement. Ceci sera encore plus vrai dans la prochaine version de GCC qui utilisera un backend SSA unifié.
: ce code c'était pour optimiser et m'amuser aussi ...
unknown@K ~ $ cat test1.c int GetNb(char c, char *str) { for (int i=0; ;str++,i++) (*str ? ((*str == c) ? ({return (i);}) : ({continue;}) ) : ({return (-1);})); return -1; } unknown@K ~ $ gcc -S -Os -fomit-frame-pointer -std=gnu99 test1.c unknown@K ~ $ cat test1.s .file "test1.c" .text .align 2 .globl _GetNb .def _GetNb; .scl 2; .type 32; .endef _GetNb: pushl %ebx pushl %ecx movb 12(%esp), %al movl 16(%esp), %edx movb %al, 3(%esp) xorl %eax, %eax L2: movb (%edx), %cl orl $-1, %ebx testb %cl, %cl je L1 movl %eax, %ebx cmpb 3(%esp), %cl je L1 incl %edx incl %eax jmp L2 L1: popl %edx movl %ebx, %eax popl %ebx ret unknown@K ~ $ cat test2.c int GetNb(char c, char *str) { for (int i=0; ;str++,i++) if (*str) if (*str == c) return (i); else continue; else return (-1); return -1; } unknown@K ~ $ gcc -S -Os -fomit-frame-pointer -std=gnu99 test2.c unknown@K ~ $ cat test2.s .file "test2.c" .text .align 2 .globl _GetNb .def _GetNb; .scl 2; .type 32; .endef _GetNb: pushl %ebx pushl %ecx movb 12(%esp), %al movl 16(%esp), %edx movb %al, 3(%esp) xorl %eax, %eax L2: movb (%edx), %cl orl $-1, %ebx testb %cl, %cl je L1 movl %eax, %ebx cmpb 3(%esp), %cl je L1 incl %edx incl %eax jmp L2 L1: popl %edx movl %ebx, %eax popl %ebx ret unknown@K ~ $ diff -u test1.s test2.s --- test1.s Thu Jan 15 05:15:26 2004 +++ test2.s Thu Jan 15 05:15:46 2004 @@ -1,4 +1,4 @@ - .file "test1.c" + .file "test2.c" .text .align 2 .globl _GetNb
Sally :
nEUrOO > non ! ça marche parce que les pointeurs sont des char*, tout simplement.
Mais si tu avais une chaîne de caractères 16-bit (toujours terminée par un 0), tu pourrais utiliser exactement le même code en remplaçant char par short, et ça marcherait. ptr++ augmente la valeur de ptr de sizeof (*ptr)