Changement de disposition

This commit is contained in:
2023-09-14 15:12:21 +02:00
parent 598495d9f0
commit d8f9b69558
20 changed files with 2 additions and 2 deletions

68
TP1/Exo5/alignement.c Normal file
View File

@@ -0,0 +1,68 @@
/* alignement et objets */
struct exemple1 {
int x;
int y;
int z;
int w;
};
struct exemple2 {
char x;
char y;
char z;
char w;
};
struct exemple3 {
int x;
int y;
char z;
char w;
};
struct exemple4 {
int x;
char y;
int z;
char w;
};
union exemple5 {
int x;
char y;
int z;
char w;
};
int main()
{
int a[4] = {1,2,3,4};
char c[4] = {'a','b','c','d'};
struct exemple1 ex1 = {1,2,3,4};
struct exemple2 ex2 = {'a','b','c','d'};
struct exemple3 ex3 = {1,2,'c','d'};
struct exemple4 ex4 = {1,'c',2,'d'};
union exemple5 ex5;
int x = 61;
char y = 62;
int z = 63;
char w = 64;
ex5.x=62;ex5.y=63;ex5.z=64;ex5.w=65;
// appelez hexdump pour chaque variable
hexdump(a, sizeof(a));
hexdump(c, sizeof(c));
hexdump(&ex1, sizeof(ex1));
hexdump(&ex2, sizeof(ex2));
hexdump(&ex3, sizeof(ex3));
hexdump(&ex4, sizeof(ex4));
hexdump(&ex5, sizeof(ex5));
hexdump(&x, sizeof(x));
hexdump(&y, sizeof(y));
hexdump(&z, sizeof(z));
hexdump(&w, sizeof(w));
return 0;
}

37
TP1/Exo5/exo5.c Normal file
View File

@@ -0,0 +1,37 @@
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
void hexdump(void *ptr, size_t size) {
unsigned char *p = ptr;
unsigned char *end = p + size;
char ascii[17];
size_t i;
for (i = 0; i < 16; ++i) {
ascii[i] = '\0';
}
i = 0;
while (p != end) {
if (i % 16 == 0) {
if (i != 0) {
printf(" |%s|\n", ascii);
}
printf("%p ", p);
}
printf("%02X ", *p);
if (*p >= ' ' && *p <= '~') {
ascii[i % 16] = *p;
} else {
ascii[i % 16] = '.';
}
++p;
++i;
}
while (i % 16 != 0) {
printf(" ");
++i;
}
printf(" |%s|\n", ascii);
}