Chapitre 12 : solutions
#
Exemples
#
01_Structure
#
#include <stdio.h>
#include <string.h>
struct patient_file
{
char name[25];
int age;
float height, weight;
};
int main(void)
{
struct patient_file patient_a = {"Dupont", 25, 180.0f, 85.0f};
struct patient_file patient_b;
printf("patient_a.name: %s\n", patient_a.name);
patient_b = patient_a;
printf("patient_a.name: %s\n", patient_a.name);
strcpy(patient_a.name, "Toto");
printf("patient_a.name: %s\n", patient_a.name);
printf("patient_b.name: %s\n", patient_b.name);
// ERROR: we should use strcpy here
// clientA.nom = clientB.nom;
patient_a.age = 2 * patient_b.age;
return 0;
}
02_StructureReturn
#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct patient_file
{
char nom[25];
int age;
float taille, poids;
};
// 2 functions returning an address to a structure:
struct patient_file *function1();
struct patient_file *function2();
int main(void)
{
struct patient_file patient_a = {"Dupont", 25, 180.0f, 85.0f};
struct patient_file *ptr_patient_b;
ptr_patient_b = function1();
printf("%s\n", ptr_patient_b->nom);
ptr_patient_b = function2();
printf("%s\n", ptr_patient_b->nom);
// int a,b=12,c[100]={1,1,1,1,1,1}; // to force having new variables in the stack
printf("%s\n", ptr_patient_b->nom);
return 0;
}
struct patient_file *function1()
{
struct patient_file *ptr;
ptr = malloc(sizeof(struct patient_file));
strcpy(ptr->nom, "maFonction");
// CORRECT: returns a pointer to a variable dynamically allocated
// => so it stays in memory after we returned
return ptr;
}
struct patient_file *function2()
{
struct patient_file patient = {"Dupont", 25, 180.0f, 85.0f};
struct patient_file *ptr;
ptr = &patient;
printf("%s\n", ptr->nom);
// WRONG: returns a pointer to a variable automatically allocated
// => the variable is deleted just after we returned
// => we have a pointer pointing to some meaningless memory now!
// VS: warning C4172: returning address of local variable or temporary: client
// return &patient;
// GCC: will not warn even when using options "-Wall -Wextra".
// But it will when using optimization with option "-O2"
// => Strange behavior to say the least.
return ptr;
}
03_BitFields
#
#include <stdio.h>
// Structure of bitfields (43 bits)
// So 6 bytes in total normally!
// But data are aligned onto 4 bytes so total size is 8 bytes.
struct bitfield
{
int i : 3;
int j : 10;
int k : 30;
};
// Test alignment
struct bitfieldshort
{
int i : 1;
int j : 1;
int k : 1;
};
int main(void)
{
struct bitfield bf;
bf.i = 8; // In binary: 1000
// But we only reserved 3 bits!
// The additional bit is discarded.
bf.j = 2;
bf.k = 3;
printf("sizeof(bf) : %d\n", (int)sizeof(bf));
printf("sizeof(struct bitfield) : %d\n", (int)sizeof(struct bitfield));
printf("bf.i = %d\n", bf.i);
printf("bf.j = %d\n", bf.j);
printf("bf.k = %d\n", bf.k);
printf("sizeof(bitfieldshort) : %d\n", (int)sizeof(struct bitfieldshort));
return 0;
}
04_Union
#
#include <stdio.h>
union Union1
{
int i;
float f;
};
union Union2
{
short s;
char c[2];
};
union Union3
{
int integer;
char n[4];
};
int main(void)
{
union Union1 u;
u.f = 1.2;
printf("u.f = %f\n", u.f);
printf("u.i (Hex.) = %x\n", u.i);
printf("u.i (Dec.) = %d\n\n", u.i);
union Union2 u2;
u2.s = 64;
printf("u2.s = %d, u2.c[0] = %d, u2.c[1] = %d\n", u2.s, u2.c[0], u2.c[1]);
u2.c[0] = 'A';
u2.c[1] = 'B';
printf("u2.s = %d, u2.c[0] = %d, u2.c[1] = %d\n", u2.s, u2.c[0], u2.c[1]);
union Union3 u3;
u3.integer = 0x04030201;
printf("\n\nu3.integer = %x\n", u3.integer);
printf("u3.n[0], u3.n[1], u3.n[2], u3.n[3] = %d, %d, %d, %d\n", u3.n[0], u3.n[1], u3.n[2], u3.n[3]);
printf("\nAddresses:\n");
printf("\n\n&u3.integer = %p\n", &u3.integer);
printf("&u3.n[0], &u3.n[1], &u3.n[2], &u3.n[3] = %p, %p, %p, %p\n", &u3.n[0], &u3.n[1], &u3.n[2], &u3.n[3]);
return 0;
}
Solutions
#