C snippet for mabe
This commit is contained in:
70
prout.c
Normal file
70
prout.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct structure {
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
|
||||
struct structure_mixte {
|
||||
char a;
|
||||
int b;
|
||||
};
|
||||
|
||||
int main(){
|
||||
// simple
|
||||
int val = 15;
|
||||
int* ad = &val;
|
||||
*ad = 16;
|
||||
printf("%d\n", val);
|
||||
|
||||
// structures
|
||||
printf("taille : %d\n", sizeof(struct structure));
|
||||
struct structure* s = malloc(sizeof(struct structure));
|
||||
s->a = 17;
|
||||
s->b = 18;
|
||||
ad = (int*) s;
|
||||
printf(" ad %d\n", *ad);
|
||||
ad = ad + 1;
|
||||
printf("ad +1 %d\n", *ad);
|
||||
|
||||
// structures mixtes
|
||||
printf("taille : %d\n", sizeof(struct structure_mixte));
|
||||
struct structure_mixte* m = malloc(sizeof(struct structure_mixte));
|
||||
m->a = 'a';
|
||||
m->b = 19;
|
||||
ad = (char*) m;
|
||||
printf("ad %c\n", *(char*)ad);
|
||||
ad = (char*) ad + 1;
|
||||
printf("ad +1 %d\n", *ad);
|
||||
ad = (char*) ad + 3;
|
||||
printf("ad +3 %d\n", *ad);
|
||||
ad = (int*) m;
|
||||
*ad = 20;
|
||||
printf("%d\n", *ad);
|
||||
|
||||
// Jouer avec les adresses
|
||||
int* a = malloc(4);
|
||||
*a = 1;
|
||||
int* b = &a;
|
||||
**( int** ) b = 2;
|
||||
printf("%d\n", *a);
|
||||
free(( void* ) * ( void** ) b);
|
||||
|
||||
// comprendre les tableaux
|
||||
a = malloc(8);
|
||||
*a = 5;
|
||||
b = a + 1;
|
||||
*b = 6;
|
||||
printf("%d\n", *a);
|
||||
printf("%d\n", *b);
|
||||
|
||||
// comprendre le déférencement
|
||||
char *c = (char*) a + 1;
|
||||
*c = 'a';
|
||||
printf("%d\n", *a);
|
||||
printf("%c\n", *c);
|
||||
*a = 5;
|
||||
printf("%d\n", *a);
|
||||
free(a);
|
||||
}
|
||||
Reference in New Issue
Block a user