« Programmation C/Types avancés » : différence entre les versions

Contenu supprimé Contenu ajouté
indentation uniforme dans tout le document (avant mélangeait 1, 4 et 8, espaces et tabulations)
Ligne 5 :
<source lang="c">
struct ma_structure {
type1 champ1;
type2 champ2;
...
typeN champN;
} var1, var2, ..., varM;
</source>
Ligne 19 :
<source lang="c">
struct complexe {
int reel;
int imaginaire;
} c;
 
Ligne 133 :
{
/* en C99 */
/*
/* printf("L'offset de 'champ2' vaut %zu.\n", offsetof(struct ma_structure, champ2)); */
printf("L'offset de 'champ2' vaut %zu.\n",
/* printf("L'offset de 'champ2' vaut %zu.\n", offsetof(struct ma_structure, champ2)); */
*/
/* en C90 */
printf("L'offset de 'champ2' vaut %lu.\n",
(unsigned long) offsetof(struct ma_structure, champ2));
return 0;
}
Ligne 149 ⟶ 153 :
imaginons 2 structures:
<source lang="c">
struct str {
char *string;
};
struct str_ok {
char *string;
size_t len;
};
</source>
quelque soit la structure utilisé, on peut avec l'addresse et la cast (struct str *) accéder à la chaine.
<source lang="c">
int main (void)
{
struct str s = { "chaine" };
struct str_ok sok = { "chaine", 0 };
void *p = &sok;
if (((struct str *)p)->string)
{
{
sok.len = strlen(sok.string);
printf("%s\n", ((struct str *)p)->string);
}
}
/* en revanche: Peut-être très dangeureux !!! */
p = &s;
printf("%s\n",((struct str_ok *)p)->string);
return 0;
}
</source>
<references />
Ligne 208 ⟶ 212 :
union type_union
{
type1 champ1;
type2 champ2;
/* ... */
typeN champN;
};
 
Ligne 225 ⟶ 229 :
union _XEvent
{
int type;
 
XAnyEvent xany;
XKeyEvent xkey;
XButtonEvent xbutton;
XMotionEvent xmotion;
XCrossingEvent xcrossing;
XFocusChangeEvent xfocus;
XExposeEvent xexpose;
/* ... */
XErrorEvent xerror;
XKeymapEvent xkeymap;
long pad[24];
};
</source>
Ligne 244 ⟶ 248 :
 
<source lang="c">
XEvent ev;
XNextEvent(display, &ev);
 
switch (ev.type) {
case ButtonPress:
printf("Le bouton %d a été pressé.\n", ev.xbutton.button);
break;
default:
printf("Message type %d\n", ev.type);
}
}
</source>
 
Ligne 261 ⟶ 265 :
#include <stdio.h>
#include <string.h>
 
struct s1 {
char *string;
} s1;
struct s2{
 
char *string;
struct s2 {
size_t len;
int ______char *string;
int setnullsize_t len;
int ______;
}s2;
int setnull;
union str{
struct} s2 s_;
 
struct s1 _s;
union str {
struct s2 s_;
struct s1 _s;
}str;
 
typedef void * STR_s1;
typedef void * STR_s2;
 
STR_s2
string_analyse(STR_s1 *s)
{
static union str us, init_us = {{NULL,0,0,0}};
memcpy((void *)&us, (void *)&init_us, sizeof(union str));
if (!((struct s1 *)s)->string)
{
{
return &us._s;
}
}
us.s_.len = strlen(((struct s1 *)s)->string);
if (!us.s_.len)
{
{
us.s_.setnull = 1;
return &us._s;
}
}
us.s_.string = ((struct s1 *)s)->string;
return &us.s_;
}
 
int main(void)
{
struct s1 s = {NULL}, s_ = {"salut"}, s__ = {""};
void *p;
p = string_analyse((void *)&s);
printf("null? %s,%lu,%i\n",
((struct s2 *)p)->string, ((struct s2 *)p)->len, ((struct s2 *)p)->setnull);
p = string_analyse((void *)&s_);
printf("null? %s,%lu,%i\n",
((struct s2 *)p)->string, ((struct s2 *)p)->len, ((struct s2 *)p)->setnull);
p = string_analyse((void *)&s__);
printf("null? %s,%lu,%i\n",
((struct s2 *)p)->string, ((struct s2 *)p)->len, ((struct s2 *)p)->setnull);
printf("Réalisé en toute sécurité...\nMêmen"
"Même pour un petit vaisseau serial killer du monde d'en haut.\n");
return 0;
}
</source>
 
Tout en permettant l'optimisation:
 
<source lang="c">
struct s1{
size_t len;
char *str;
} s1;
 
union s {
struct s1 dbl[2];
void struct s1 *performdbl[52];
void *perform[5];
struct s1 simple;
struct s1 dbl[2]simple;
};
</source>
Ligne 349 ⟶ 366 :
</tr>
</table>
 
ce qui signifie que:
 
<source lang="c">
int main(void)
{
const char *end[2] = {", ", "\n"};
union s init = {{{5,"Salut"}, {3,"toi"}}}, us;
void *p;
memset((void *)&us, 0,40);
memcpy((void *)&us,
(void *)&init,
2 * 2*sizeof(struct s1));
for ( p = &us;
((union s *)p)->perform[0] != (void *)0;
p += 16)
) printf("%s%s",
(char *)((union s *)p)->perform[1],
end[
!((char *)((union s *)p)->perform[2])]
);
); return 0;
}
</source>
Ligne 461 ⟶ 481 :
struct liste
{
struct liste * suivant;
struct liste * precedant;
void * element;
};
</source>
Ligne 475 ⟶ 495 :
struct type_a
{
struct type_a * champ1;
struct type_b * champ2;
int champ3;
};
 
struct type_b
{
struct type_a * ref;
void * element;
};
</source>