//
//  struct_pack.c
//
//  Created by Julien-Pierre Avrous on 16/10/06.
//  Copyright 2006 SourceMac.com. All rights reserved.
//
//

#include <stdio.h>

/*
** Structure avec champs non allignés
*/

typedef struct {
	int i;
	char k;
	int v;
} __attribute__ ((packed)) s_packed;


/*
** Structure avec alignement sur mot mémoire
*/
typedef struct {
	int i;
	char k;
	int v;
} s_unpacked;



/*
** Main d'exemple
*/
int main()
{
	s_packed	pack = {3, 'a', 5};
	s_unpacked	unpack = {3, 'a', 5};

	
	FILE *f1;
	FILE *f2;
	
	f1 = fopen("packed", "w+");
	f2 = fopen("unpacked", "w+");
	
	fwrite(&pack, sizeof(pack), 1, f1);
	fwrite(&unpack, sizeof(unpack), 1, f2);
	
	printf("Averous Julien-Pierre - 2006 - www.sourcemac.com\n");
	printf("Size packed = %i\n", ftell(f1));
	printf("Size unpacked = %i\n", ftell(f2));
	
	fclose(f1);
	fclose(f2);
	
	return (0);
}