|
24バイトのメモリエリアのフォーマットを定義しています
|
|
|
char Layout[24];
int i;
for( i = 0; i < sizeof(Layout); i++ ) {
Layout[i] = 0x00;
}
| |
|
|
|
|
*((long *)Layout+0) = 101;
*((short *)(Layout+4)) = 102;
*((short *)(Layout+6)) = 103;
*(Layout+8) = 'A';
*(Layout+8+1) = 'B';
*(Layout+8+2) = 'C';
*((long *)(Layout+20)) = 104;
| |
|
|
|
|
struct LAYOUT {
long a;
short b;
short c;
char d[12];
long e;
} *lo;
lo = (struct LAYOUT *)Layout;
printf( "%ld\n", lo->a );
printf( "%d\n", lo->b );
printf( "%d\n", lo->c );
printf( "%s\n", lo->d );
printf( "%ld\n", lo->e );
| |
|
|
|
|
#include <string.h>
#include <memory.h>
が必要です
| |
|
|
typedef struct _LAYOUT {
long a;
short b;
short c;
char d[12];
long e;
} LAYOUT;
LAYOUT Layout;
memset( &Layout, 0x00, sizeof( Layout ) );
Layout.a = 101;
Layout.b = 102;
Layout.c = 103;
strcpy( Layout.d, "ABC" );
Layout.e = 104;
printf( "%ld\n", Layout.a );
printf( "%d\n", Layout.b );
printf( "%d\n", Layout.c );
printf( "%s\n", Layout.d );
printf( "%ld\n", Layout.e );
| |
|
|
|
|
short Layout[12];
int i;
for( i = 0; i < sizeof(Layout)/2; i++ ) {
Layout[i] = 0x0000;
}
*((long *)Layout+0) = 101;
*(Layout+2) = 102;
*(Layout+3) = 103;
*(Layout+4) = 0x4241;
*(Layout+5) = 0x0043;
*((long *)(Layout+10)) = 104;
struct LAYOUT {
long a;
short b;
short c;
char d[12];
long e;
} *lo;
lo = (struct LAYOUT *)Layout;
printf( "%ld\n", lo->a );
printf( "%d\n", lo->b );
printf( "%d\n", lo->c );
printf( "%s\n", lo->d );
printf( "%ld\n", lo->e );
| |
|
|
|
|
long Layout[6];
int i;
for( i = 0; i < sizeof(Layout)/4; i++ ) {
Layout[i] = 0x00000000;
}
*(Layout+0) = 101;
*(Layout+1) = 0x00670066;
*(Layout+2) = 0x00434241;
*(Layout+5) = 104;
struct LAYOUT {
long a;
short b;
short c;
char d[12];
long e;
} *lo;
lo = (struct LAYOUT *)Layout;
printf( "%ld\n", lo->a );
printf( "%d\n", lo->b );
printf( "%d\n", lo->c );
printf( "%s\n", lo->d );
printf( "%ld\n", lo->e );
| |
|
|
|
|
#include <memory.h>
// 構造体の定義
struct LAYOUT {
long a;
short b;
short c;
char d[12];
long e;
};
// 関数の宣言
void test( struct LAYOUT * );
int main()
{
long Layout[6];
int i;
for( i = 0; i < sizeof(Layout)/4; i++ ) {
Layout[i] = 0x00000000;
}
*(Layout+0) = 101;
*(Layout+1) = 0x00670066;
*(Layout+2) = 0x00434241;
*(Layout+5) = 104;
// 構造体の定義 (メモリエリア確保)
struct LAYOUT lo;
// Layout より構造体へデータをコピー
memcpy( &lo, Layout, sizeof( struct LAYOUT ) );
// 関数呼び出し
test( &lo );
return 0;
}
void test( struct LAYOUT *a )
{
printf( "%ld\n", a->a );
printf( "%d\n", a->b );
printf( "%d\n", a->c );
printf( "%s\n", a->d );
printf( "%ld\n", a->e );
}
| |
|
|
|
|
#include <memory.h>
// _LAYOUT はなんでもかまいませんが、偶然使われる可能性の低い文字列を指定します
// この記述によって、データ型 LAYOUT が定義されます
typedef struct _LAYOUT {
long a;
short b;
short c;
char d[12];
long e;
} LAYOUT;
void test( LAYOUT * );
int main()
{
long Layout[6];
int i;
for( i = 0; i < sizeof(Layout)/4; i++ ) {
Layout[i] = 0x00000000;
}
*(Layout+0) = 101;
*(Layout+1) = 0x00670066;
*(Layout+2) = 0x00434241;
*(Layout+5) = 104;
LAYOUT lo;
memcpy( &lo, Layout, sizeof( LAYOUT ) );
test( &lo );
return 0;
}
void test( LAYOUT *a )
{
printf( "%ld\n", a->a );
printf( "%d\n", a->b );
printf( "%d\n", a->c );
printf( "%s\n", a->d );
printf( "%ld\n", a->e );
}
| |
|
|
|