第9話 構造体
type struct01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* 基本的な使用法
*/
#include <stdio.h>
#include <string.h>
struct rcd
{
char name[16];
char hobby[32];
int id;
char sex;
};
int main(void)
{
struct rcd record;
strcpy(record.name, "sato");
strcpy(record.hobby, "play tennis");
record.id=0x01020304;
record.sex='M';
printf("record.name =%s\n",record.name);
printf("record.hobby=%s\n",record.hobby);
printf("record.id =0x%08X\n",record.id);
printf("record.sex =%C\n",record.sex);
return 0;
}
type struct02.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* 構造体のポインタ
*/
#include <stdio.h>
#include <string.h>
struct rcd
{
char name[16];
char hobby[32];
int id;
char sex;
};
void sub(struct rcd *record)
{
printf("record.name =%s\n",record->name);
printf("record.hobby=%s\n",record->hobby);
printf("record.id =0x%08X\n",record->id);
printf("record.sex =%C\n",record->sex);
}
int main(void)
{
struct rcd record;
strcpy(record.name, "sato");
strcpy(record.hobby, "play tennis");
record.id=0x01020304;
record.sex='M';
sub(&record);
return 0;
}
type struct03.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* 構造体初期化&代入
*/
#include <stdio.h>
struct rcd
{
char name[16];
char hobby[32];
int id;
char sex;
};
int main(void)
{
struct rcd record ={"sato","play tennis",0x01020304,'M'};
struct rcd dest;
dest=record; /*代入*/
printf("dest.name =%s\n", dest.name);
printf("dest.hobby=%s\n", dest.hobby);
printf("dest.id =0x%08X\n",dest.id);
printf("dest.sex =%C\n", dest.sex);
return 0;
}
type struct04.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* 構造体初期化&代入の別の例
*/
#include <stdio.h>
struct rcd
{
char name[16];
};
int main(void)
{
struct rcd record ={"sato"};
struct rcd dest;
/* dest,name=record.name; エラー*/
dest=record; /*代入*/
printf("dest.name =%s\n", dest.name);
return 0;
}
type struct05.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* 構造体初期化&代入
*/
#include <stdio.h>
#include <string.h>
struct rcd
{
char name[16];
char hobby[32];
int id;
char sex;
};
int main(void)
{
FILE* fp ;
struct rcd record;
struct rcd dest;
memset(&record,0xAA,sizeof(dest));
/* 書き込み */
if( (fp = fopen( "record.bin", "wb" )) == NULL )
return -1;
fwrite((void*)&record, sizeof(record),1,fp);
fclose( fp ) ;
strcpy(record.name, "sato");
strcpy(record.hobby, "play tennis");
record.id=0x01020304;
record.sex='M';
/* 書き込み */
if( (fp = fopen( "01.bin", "wb" )) == NULL )
return -1;
fwrite((void*)&record, sizeof(record),1,fp);
fclose( fp ) ;
memset(&dest,0xFF,sizeof(dest));
strcpy(dest.name, record.name);
strcpy(dest.hobby, record.hobby);
dest.id=record.id;
dest.sex=record.sex;
/* 書き込み */
if( (fp = fopen( "02.bin", "wb" )) == NULL )
return -1;
fwrite((void*)&dest, sizeof(dest),1,fp);
fclose( fp ) ;
memset(&dest,0xFF,sizeof(dest));
dest=record; /*代入*/
/* 書き込み */
if( (fp = fopen( "03.bin", "wb" )) == NULL )
return -1;
fwrite((void*)&dest, sizeof(dest),1,fp);
fclose( fp ) ;
return 0;
}
type struct06.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* 構造体初期化&代入,丸ごとコピー
*/
#include <stdio.h>
#include <string.h>
struct rcd
{
char name[16];
char hobby[32];
int id;
char sex;
};
int main(void)
{
struct rcd record ={"sato","play tennis",0x01020304,'M'};
struct rcd dest0;
struct rcd dest1;
dest0=record; /*構文で代入*/
memcpy(&dest1,&record,sizeof(dest1)); /*標準関数で代入*/
printf("dest0.name =%s\n", dest0.name);
printf("dest0.hobby=%s\n", dest0.hobby);
printf("dest0.id =0x%08X\n",dest0.id);
printf("dest0.sex =%C\n", dest0.sex);
printf("dest1.name =%s\n", dest1.name);
printf("dest1.hobby=%s\n", dest1.hobby);
printf("dest1.id =0x%08X\n",dest1.id);
printf("dest1.sex =%C\n", dest1.sex);
return 0;
}
type struct07.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* 構造体配列と初期化&代入
*/
#include <stdio.h>
#include <string.h>
#define MAX_ARRAY 4
struct rcd
{
char name[16];
char hobby[32];
int id;
char sex;
};
int main(void)
{
struct rcd record[MAX_ARRAY] ={
{"sato","play tennis",0x01020304,'M'},
{"tanaka","watch television",1,'F'},
{"suzuki","listen to music",0x12345678,'M'},
{"taro","skiing",2,'F'}
};
struct rcd dest[MAX_ARRAY];
int i;
memcpy(dest,record,sizeof(record)); /*代入*/
/* 出力 */
for(i=0;i<sizeof(record)/sizeof(record[0]);i++)
{
printf("dest[%d].name=%s\n", i,dest[i].name);
printf("dest[%d].hobby=%s\n", i,dest[i].hobby);
printf("dest[%d].id=0x%08X\n",i,dest[i].id);
printf("dest[%d].sex=%C\n", i,dest[i].sex);
printf("\n");
}
return 0;
}
type struct08.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* 実体を取る
*/
#include <stdio.h>
#include <string.h>
int main(void)
{
struct
{
char name[16];
char hobby[32];
int id;
char sex;
} record ;
strcpy(record.name, "sato");
strcpy(record.hobby, "play tennis");
record.id=0x01020304;
record.sex='M';
printf("record.name =%s\n",record.name);
printf("record.hobby=%s\n",record.hobby);
printf("record.id =0x%08X\n",record.id);
printf("record.sex =%C\n",record.sex);
return 0;
}
type struct09.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* typedef
*/
#include <stdio.h>
typedef struct
{
char name[16];
char hobby[32];
int id;
char sex;
} rcd ;
int main(void)
{
rcd record ={"sato","play tennis",0x01020304,'M'};
rcd dest;
dest=record; /*代入*/
printf("dest.name =%s\n", dest.name);
printf("dest.hobby=%s\n", dest.hobby);
printf("dest.id =0x%08X\n",dest.id);
printf("dest.sex =%C\n", dest.sex);
return 0;
}
type struct10.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* アライメント指定(Borland C++ Compilerでは-a1
* で大きさが変わる
*/
#include <stdio.h>
struct rcd
{
char name[16];
char hobby[32];
int id;
char sex;
};
int main(void)
{
struct rcd record;
printf("size of record=%d bytes.\n",sizeof(record));
return 0;
}
-------------------------------
type structAlign01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* アライメント指定(Borland C++ Compilerでは-a1
* で大きさが変わる
*/
#include <stdio.h>
#include <stddef.h>
struct tag_alignment
{
char c0;
short s;
char c1;
char c2;
int i;
char c3;
};
int main(void)
{
struct tag_alignment alignment;
printf("size of struct tag_alignment=%d\n", sizeof(struct tag_alignment));
printf("offset of c0=%d, size of c0=%d\n",
offsetof(struct tag_alignment, c0), sizeof(alignment.c0));
printf("offset of s=%d, size of s=%d\n",
offsetof(struct tag_alignment, s), sizeof(alignment.s));
printf("offset of c1=%d, size of c1=%d\n",
offsetof(struct tag_alignment, c1), sizeof(alignment.c1));
printf("offset of c2=%d, size of c2=%d\n",
offsetof(struct tag_alignment, c2), sizeof(alignment.c2));
printf("offset of i=%d, size of i=%d\n",
offsetof(struct tag_alignment, i), sizeof(alignment.i));
printf("offset of c3=%d, size of c3=%d\n",
offsetof(struct tag_alignment, c3), sizeof(alignment.c3));
return 0;
}
type structbits01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
* ビットの構造体
*/
#include <stdio.h>
#include <string.h>
#if 1
struct busreset_bits /* Bus reset register (Little Endian) */
{
unsigned int nridval:1,
rsvd1:1,
nodecount:6,
root:1,
contndr:1,
irmnodeid:6,
busnumber:10,
nodenumber:6 ;
};
#else
struct busreset_bits /* Bus reset register (Big Endian) */
{
unsigned int nodenumber:6,
busnumber:10,
irmnodeid:6,
contndr:1,
root:1,
nodecount:6,
rsvd1:1,
nridval:1 ;
};
#endif
int main(void)
{
struct busreset_bits br;
memset(&br,0,4);
printf("br=0x%08X\n",br);
br.root = 1;
br.nodenumber = 63;
br.busnumber = 1023;
printf("br=0x%08X\n",br);
return 0;
}