How to initialize an array of structures within a Microsoft C program (75602)



The information in this article applies to:

  • Microsoft C/C++ compiler for MS-DOS
  • Microsoft Visual C++ 1.0
  • Microsoft Visual C++ 1.5
  • Microsoft Visual C++ 2.0
  • Microsoft Visual C++ 4.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q75602

SUMMARY

The code fragment below demonstrates how to initialize an array of structures within a Microsoft C program. Each element is grouped within brackets, and the elements are separated by commas. The initialization of the array rgttype shows how to initialize a structure within a structure within an array of structures.

Sample Code

/* Compile options needed:  none
*/ 

struct stype {
   int a;
   int b;
   int c;
};

struct ttype {
   int alpha;
   struct stype beta;
};

                        /*   a,    b,   c  */ 
struct stype rgstype[2] = { {8,   9,   10}, \ 
                            {15,  16,  17} };

                        /*  alpha     beta     */ 
struct ttype rgttype[2] = { {{1},   {2,3,4}}, \ 
                            {{5},   {6,7,8}}  };
				

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kbhowto kbcode kbfile kbLangC KB75602 kbAudDeveloper