構造体とメモリ

  構造体の大きさ



  
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>

struct MYDATA {
	char	type_1;
	short	type_2;
	long	type_3;
	int	type_4;
	float	type_5;
	double	type_6;
};

int main( int argc, char *argv[] )
{

	struct	MYDATA md;
	int	allsize;

	printf( "MYDATA の大きさは %d\n", sizeof( struct MYDATA ) );

	allsize = 
		sizeof( char ) + 
		sizeof( short ) + 
		sizeof( long ) +
		sizeof( int ) + 
		sizeof( float ) + 
		sizeof( double );

	printf( "MDATA のメンバの大きさの合計は %d\n", allsize );

	return 0;
}
  

  
MYDATA の大きさは 24
MDATA のメンバの大きさの合計は 23
  



  初期化しない構造体の中身



  
#include <stdio.h>

struct MYDATA {
	char	type_1;
	short	type_2;
	long	type_3;
	int	type_4;
	float	type_5;
	double	type_6;
};

int main( int argc, char *argv[] )
{

	struct	MYDATA md;
	FILE *fp;

	fp = fopen( "MYDATA", "wb" );
	if ( fp == NULL ) {
		return 1;
	}

	fwrite( &md, sizeof( struct MYDATA ), 1, fp );

	fclose( fp );

	return 0;
}
  

          0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
--------------------------------------------------------------------------
00000000 D4 B5 F5 77 30 A7 E3 77 68 07 00 00 2B 85 09 61  ヤオ.w0ァ縢h...+..a
00000010 68 07 00 00 FF FF FF FF                          h.......




  初期化した構造体の中身

  
#include <stdio.h>

struct MYDATA {
	char	type_1;
	short	type_2;
	long	type_3;
	int	type_4;
	float	type_5;
	double	type_6;
};

int main( int argc, char *argv[] )
{

	struct	MYDATA md;
	FILE *fp;

	fp = fopen( "MYDATA", "wb" );
	if ( fp == NULL ) {
		return 1;
	}

	md.type_1 = 0;
	md.type_2 = 0;
	md.type_3 = 0;
	md.type_4 = 0;
	md.type_5 = 0;
	md.type_6 = 0;

	fwrite( &md, sizeof( struct MYDATA ), 1, fp );

	fclose( fp );

	return 0;
}
  

          0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
--------------------------------------------------------------------------
00000000 00 B5 00 00 00 00 00 00 00 00 00 00 00 00 00 00  .オ..............
00000010 00 00 00 00 00 00 00 00                          ........



  値をセットした構造体の中身

  
#include <stdio.h>

struct MYDATA {
	char	type_1;
	short	type_2;
	long	type_3;
	int	type_4;
	float	type_5;
	double	type_6;
};

int main( int argc, char *argv[] )
{

	struct	MYDATA md;
	FILE *fp;

	fp = fopen( "MYDATA", "wb" );
	if ( fp == NULL ) {
		return 1;
	}

	md.type_1 = 8;
	md.type_2 = 9;
	md.type_3 = 10;
	md.type_4 = 11;
	md.type_5 = 12;
	md.type_6 = 13;

	fwrite( &md, sizeof( struct MYDATA ), 1, fp );

	fclose( fp );

	return 0;
}
  

          0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
--------------------------------------------------------------------------
00000000 08 B5 09 00 0A 00 00 00 0B 00 00 00 00 00 40 41  .オ............@A
00000010 00 00 00 00 00 00 2A 40                          ......*@



  関数に引き渡された構造体

  
#include <stdio.h>

struct MYDATA {
	char	type_1;
	short	type_2;
	long	type_3;
	int	type_4;
	float	type_5;
	double	type_6;
};

void WriteMyData( struct MYDATA *pmd, FILE *fp );

int main( int argc, char *argv[] )
{

	struct	MYDATA md;
	FILE *fp;

	fp = fopen( "MYDATA", "wb" );
	if ( fp == NULL ) {
		return 1;
	}

	WriteMyData( &md, fp );

	fclose( fp );

	return 0;
}

void WriteMyData( struct MYDATA *pmd, FILE *fp )
{

	pmd->type_1 = 8;
	pmd->type_2 = 9;
	pmd->type_3 = 10;
	pmd->type_4 = 11;
	pmd->type_5 = 12;
	pmd->type_6 = 13;

	fwrite( pmd, sizeof( struct MYDATA ), 1, fp );

}
  










  infoboard   管理者用   





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ