Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Initializing an array of an array of structs 1

Status
Not open for further replies.

jspaarg

Programmer
Jun 13, 2005
1
US
Greetings,

I have the following code:

typedef struct {
int volts;
int limit;
int time;
}mystep;

typedef struct {
mystep steps[4];
}myprofile;

myprofile profiles[4];

This gives me an array[4] of an array[4] of structures (3 elements).

I cannot figure out the syntax to initialize this.

No matter what I try I get some type of error.

Any ideas are greatly appreciated.
Thanks
 
Code:
#include <stdio.h>

typedef struct
{
   int  volts;
   int  limit;
   int  time;
}mystep;

typedef struct
{
   mystep steps[4];
}myprofile;

[blue]myprofile profiles[4] =
{
   { /* profile[0] */
      {  /* steps */
         {10,11,12},  /* steps[0] */
         {13,14,15},  /* steps[1] */
         {16,17,18},  /* steps[2] */
         {19,20,21},  /* steps[3] */
      }
   },
   { /* profile[1] */
      {  /* steps */
         {22,23,24},  /* steps[0] */
         {25,26,27},  /* steps[1] */
         {28,29,30},  /* steps[2] */
         {31,32,33},  /* steps[3] */
      }
   },
   { /* profile[2] */
      {  /* steps */
         {35,36,37},  /* steps[0] */
         {38,39,40},  /* steps[1] */
         {41,42,43},  /* steps[2] */
         {44,45,46},  /* steps[3] */
      }
   },
   {  /* profile[3] */
      {  /* steps */
         {47,48,49},  /* steps[0] */
         {50,51,52},  /* steps[1] */
         {53,54,55},  /* steps[2] */
         {56,57,58},  /* steps[3] */
      }
   },
};[/blue]

int main()
{
   size_t i, j;
   for ( i = 0; i < sizeof profiles / sizeof *profiles; ++i )
   {
      for ( j = 0; j < sizeof profiles[0].steps / sizeof *profiles[0].steps; ++j )
      {
         printf("profiles[%lu].steps[%lu].volts = %d\n",
                (long unsigned)i, (long unsigned)j, profiles[i].steps[j].volts);
         printf("profiles[%lu].steps[%lu].limit = %d\n",
                (long unsigned)i, (long unsigned)j, profiles[i].steps[j].limit);
         printf("profiles[%lu].steps[%lu].time  = %d\n\n",
                (long unsigned)i, (long unsigned)j, profiles[i].steps[j].time);
      }
   }
   return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top