Subject: to demonstrate the usage of nested dynamic list (nested struct, union) in C-language (C99).
Usage: compile source C-code (for example, with M$ Visual Studio or with GCC)
and run it with one option: the name of file (like <the name of program> <the name of file>).
//------- the start of the C code -------
/*********************************************************************
* Author: Stepan A. Baranov (rosmir@gmail.com)
* web-site: www.rosmir.org
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#if defined(_MSC_VER) // M$ Visual Studio
#pragma warning (disable: 4996)
#endif
#define bl_t 1 /* boolean type */
#define in_t 2 /* integer type */
#define fl_t 3 /* float type */
#define ivl1 vl1.in
#define ivl2 vl2.in
#define ivl3 vl3.in
#define fvl1 vl1.fl
#define fvl2 vl2.fl
#define fvl3 vl3.fl
union typeValue {
int in;
float fl;
};
struct fldSpc {
unsigned int Id;
unsigned int type;
union typeValue vl1;
union typeValue vl2;
union typeValue vl3;
};
struct fldArray {
unsigned int num;
struct fldSpc *fld;
};
struct mbrArray {
unsigned int num;
struct fldArray *member;
};
int
main (int argc, const char *argv[])
{
FILE *fp;
struct mbrArray *list;
list = (struct mbrArray *) malloc (sizeof (struct mbrArray));
if (list == NULL) return EXIT_FAILURE;
list->member = (struct fldArray *) malloc (2 * sizeof (struct fldArray));
if (list->member == NULL) return EXIT_FAILURE;
else list->num = 2;
list->member[0].fld = (struct fldSpc *) malloc (2 * sizeof (struct fldSpc));
if (list->member[0].fld == NULL) return EXIT_FAILURE;
else list->member[0].num = 2;
list->member[1].fld = (struct fldSpc *) malloc (2 * sizeof (struct fldSpc));
if (list->member[1].fld == NULL) return EXIT_FAILURE;
else list->member[1].num = 2;
list->member[0].fld[1].Id = 12;
list->member[0].fld[1].type = in_t;
list->member[0].fld[1].ivl1 = 56;
list->member[0].fld[1].ivl2 = 123;
list->member[0].fld[1].ivl3 = 3;
list->member[1].fld[0].Id = 25;
list->member[1].fld[0].type = fl_t;
list->member[1].fld[0].fvl1 = 9.067f;
list->member[1].fld[0].fvl2 = 11.8765f;
list->member[1].fld[0].fvl3 = 0.342f;
if (argc != 2) return EXIT_FAILURE;
fp = fopen (argv[1], "w+b");
if (fp == NULL) return EXIT_FAILURE;
fseek (fp, 0, SEEK_SET);
fwrite (&list->member[0].fld[1], sizeof (struct fldSpc), 1, fp);
fwrite (&list->member[1].fld[0], sizeof (struct fldSpc), 1, fp);
fseek (fp, 0, SEEK_SET);
fread (&list->member[0].fld[0], sizeof (struct fldSpc), 1, fp);
fread (&list->member[1].fld[1], sizeof (struct fldSpc), 1, fp);
printf ("%d %d %d %d %d \n", list->member[0].fld[0].Id,
list->member[0].fld[0].type, list->member[0].fld[0].ivl1,
list->member[0].fld[0].ivl2, list->member[0].fld[0].ivl3);
printf ("%d %d %f %f %f \n", list->member[1].fld[1].Id,
list->member[1].fld[1].type, list->member[1].fld[1].fvl1,
list->member[1].fld[1].fvl2, list->member[1].fld[1].fvl3);
free (list->member[0].fld);
free (list->member[1].fld);
free (list->member);
free (list);
fclose (fp);
return EXIT_SUCCESS;
}
//------- the end of the C code -------
|
©2009 Rosmir - Stepan A. Baranov
$Id: nesteddynamiclist.html 413 2009-01-06 21:16:05Z rosmir $
|
|