1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
/*
* File: source.c
* Author: SAMBIA39
*
* Created on 12 novembre 2015, 10:44
*
* Le code-source qui suit est susceptible
* de comporter des erreurs.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Variable X
*/
typedef union u_var{
struct{
int _int;
char _char;
long _long;
short _short;
float _float;
void *_p_data;
double _double;
unsigned int _uint;
unsigned char _uchar;
}x;
/* autres possibilité */
/* struct x *p; */
}X_var_data;
int main( void ){
X_var_data myVar;
char *p = malloc( 100 *sizeof(char) );
if( (NULL) == p ){
fprintf( stderr, "(%d)\t:%s\n\t:%s\n", errno,
"Erreur allocation", strerror(errno) );
return EXIT_FAILURE;
}
myVar.x._int = 10;
myVar.x._char = 'D';
myVar.x._long = 1024;
myVar.x._short = 67;
myVar.x._float = 10.45;
myVar.x._p_data = p;
fprintf( stdout, "Val Type int\t=\t%d\n", myVar.x._int );
fprintf( stdout, "Val Type char\t=\t%c\n", myVar.x._char );
fprintf( stdout, "Val Type long\t=\t%ld\n", myVar.x._long );
fprintf( stdout, "Val Type short\t=\t%hd\n", myVar.x._short );
fprintf( stdout, "Val Type float\t=\t%fl\n", myVar.x._float );
fprintf( stdout, "Val Type void pointeur \t=\t%p\n", myVar.x._p_data );
free( myVar.x._p_data );
myVar.x._p_data = NULL;
fprintf( stdout, "\n>--------------<\n");
fprintf( stdout, "Val Type int\t=\t%d\n", myVar.x._int );
fprintf( stdout, "Val Type char\t=\t%c\n", myVar.x._char );
fprintf( stdout, "Val Type long\t=\t%ld\n", myVar.x._long );
fprintf( stdout, "Val Type short\t=\t%hd\n", myVar.x._short );
fprintf( stdout, "Val Type float\t=\t%fl\n", myVar.x._float );
fprintf( stdout, "Val Type void pointeur \t=\t%p\n", myVar.x._p_data );
return EXIT_SUCCESS;
} |