struct within struct

C and PASCAL (or any other high-level languages) in here please

Moderators: exxos, simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4867
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

struct within struct

Postby simonsunnyboy » Sun Feb 28, 2010 11:05 am

Hello together,

i try to declare a struct within another struct with plain C and fill an insatcne with values. Somehow I get errors if i try the method below:

Code: Select all

typedef struct
{
   unsigned short col[16];
} FadeStep;

typedef struct
{
   FadeStep FadePal[16];
} FadeSequence;

FadeSequence fade_FADE_H =
{
  /* Palette 0 */
  { 0x999, 0xF7E, 0xE7F, 0x135, 0xAC6, 0xDEF, 0xA23, 0x5C6, 0xFED, 0xAD7, 0x94E, 0xFFF, 0xB56, 0xC67, 0x2B5, 0xB67 },

  /* Palette 1 */
  { 0x112, 0x7E6, 0x6E7, 0x8AC, 0x24D, 0x567, 0x29A, 0xC4D, 0x765, 0x25E, 0x1B6, 0x777, 0x3CD, 0x4DE, 0x93C, 0x3DE },

  /* Palette 2 */
  { 0x882, 0xE6D, 0xD6E, 0x024, 0x9B5, 0xCDE, 0x912, 0x4B5, 0xEDC, 0x9C6, 0x83D, 0xEEE, 0xA45, 0xB56, 0x1A4, 0xA56 },

  /* Palette 3 */
  { 0x002, 0x6D5, 0x5D6, 0x09B, 0x13C, 0x456, 0x182, 0xB3C, 0x654, 0x14D, 0x0A5, 0x666, 0x2BC, 0x3CD, 0x82B, 0x2CD },

  /* Palette 4 */
  { 0x002, 0xD5C, 0xC5D, 0x013, 0x8A4, 0xBCD, 0x802, 0x3A4, 0xDCB, 0x8B5, 0x02C, 0xDDD, 0x934, 0xA45, 0x093, 0x945 },

  /* Palette 5 */
  { 0x002, 0x5C4, 0x4C5, 0x08A, 0x02B, 0x345, 0x002, 0xA2B, 0x543, 0x03C, 0x094, 0x555, 0x1AB, 0x2BC, 0x01A, 0x1BC },

  /* Palette 6 */
  { 0x002, 0xC4B, 0xB4C, 0x002, 0x093, 0xABC, 0x002, 0x293, 0xCBA, 0x0A4, 0x01B, 0xCCC, 0x823, 0x934, 0x082, 0x834 },

  /* Palette 7 */
  { 0x002, 0x4B3, 0x3B4, 0x002, 0x01A, 0x234, 0x002, 0x91A, 0x432, 0x02B, 0x083, 0x444, 0x09A, 0x1AB, 0x002, 0x0AB },

  /* Palette 8 */
  { 0x002, 0xB3A, 0xA3B, 0x002, 0x082, 0x9AB, 0x002, 0x182, 0xBA2, 0x093, 0x00A, 0xBBB, 0x012, 0x823, 0x002, 0x023 },

  /* Palette 9 */
  { 0x002, 0x3A2, 0x2A3, 0x002, 0x002, 0x123, 0x002, 0x802, 0x322, 0x01A, 0x002, 0x333, 0x082, 0x09A, 0x002, 0x09A },

  /* Palette 10 */
  { 0x002, 0xA22, 0x92A, 0x002, 0x002, 0x89A, 0x002, 0x002, 0xA92, 0x082, 0x002, 0xAAA, 0x002, 0x012, 0x002, 0x012 },

  /* Palette 11 */
  { 0x002, 0x292, 0x192, 0x002, 0x002, 0x012, 0x002, 0x002, 0x212, 0x002, 0x002, 0x222, 0x002, 0x082, 0x002, 0x082 },

  /* Palette 12 */
  { 0x002, 0x912, 0x812, 0x002, 0x002, 0x082, 0x002, 0x002, 0x982, 0x002, 0x002, 0x992, 0x002, 0x002, 0x002, 0x002 },

  /* Palette 13 */
  { 0x002, 0x182, 0x082, 0x002, 0x002, 0x002, 0x002, 0x002, 0x102, 0x002, 0x002, 0x112, 0x002, 0x002, 0x002, 0x002 },

  /* Palette 14 */
  { 0x002, 0x802, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x802, 0x002, 0x002, 0x882, 0x002, 0x002, 0x002, 0x002 },

  /* Palette 15 */
  { 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002 }

};


Can someone explain? it's basically a struct containing 16 subarrays.

If i inline the data in somneotherway, e.q. cast or dc.w statements, all works fine.

cheers,
ssb
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

swapd0
Atari User
Atari User
Posts: 42
Joined: Thu Dec 13, 2007 8:56 pm

Re: struct within struct

Postby swapd0 » Sun Feb 28, 2010 1:16 pm

You need to tell the compiler that fade_FADE_H is an array, so you must use the {} initializers, just put [] after fade_FADE_H

FadeSequence fade_FADE_H[] =

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4867
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: struct within struct

Postby simonsunnyboy » Sun Feb 28, 2010 6:21 pm

It is not an array, if you look closely it is of type FadeSequence which is a struct containing 16 subarrays as more structs...
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

swapd0
Atari User
Atari User
Posts: 42
Joined: Thu Dec 13, 2007 8:56 pm

Re: struct within struct

Postby swapd0 » Sun Feb 28, 2010 8:06 pm

I know that it's a struct but if you want to use initializers, the left part of the expression must be an array or pointer. Then the compiler know where the data must be copied.

When you write FadeSequence fade_FADE_H[] = { ... }; you are telling the address of the first palette to write the data there. You are not declaring an array of palettes.

Have you tried to put the []?

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4867
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: struct within struct

Postby simonsunnyboy » Mon Mar 01, 2010 6:23 pm

I tried now...but it doesn't help for accessing the data.

The compilers (AHCC + gcc) now tell me I try to access something which is not a struct or union.

E.q.

Code: Select all

FadeStep step;
step = fade_FADE_H.FadePal[4];


:oops:
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: struct within struct

Postby Nyh » Mon Mar 01, 2010 7:43 pm

simonsunnyboy wrote:i try to declare a struct within another struct with plain C and fill an insatcne with values. Somehow I get errors if i try the method below:

Can someone explain? it's basically a struct containing 16 subarrays.

If i inline the data in somneotherway, e.q. cast or dc.w statements, all works fine.

It is simple:

Initialize simple data in a struct.

Code: Select all

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

struct foo bar={1, 2, 3};


Array in a struct:

Code: Select all

struct foo2
{
  int a;
  int b[16];
};

struct foo2 bar2={1, {1,2,3,4,5,6,7,8,9}};

I think the solution to your problem will be clear to you.

Hans Wessels

Note: I didn't test the code but it should work fine.

Note 2: struct are fine but for demo effects don't overdo them. You gain more by making a special type for a colorregister data holder:

Code: Select all

typedef unsigned short int  colorregister;

And use the type colorregister where you need to access color registers and store color register data.

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4867
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: struct within struct

Postby simonsunnyboy » Mon Mar 01, 2010 7:49 pm

No I don't spot the solution. It's the same syntax that I tried with the exception that you don't typedef your structs which I don't like.

It's just the declaration of the contents - If I do an extern declaration on the same data done with dc.b in an .s file, it works 100% so there must be a way to declare that struct in C directly (if desired).

E.q.

Code: Select all

extern FadeSequence fade; /* in file A */

/* in file B.s */
fade:
dc.b x,y,z etc
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: struct within struct

Postby Nyh » Mon Mar 01, 2010 7:57 pm

simonsunnyboy wrote:No I don't spot the solution. It's the same syntax that I tried with the exception that you don't typedef your structs which I don't like.

It's just the declaration of the contents - If I do an extern declaration on the same data done with dc.b in an .s file, it works 100% so there must be a way to declare that struct in C directly (if desired).

Please have an other good look on how I initialize an array in a struct:

Code: Select all

struct foo2
{
  int a;
  int b[16];
};

struct foo2 bar2={1, {1,2,3,4,5,6,7,8,9}};

Now try to think how it would look if struct foo2 contained only the array and not the int....

Got it now?

Hans Wessels

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4867
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: struct within struct

Postby simonsunnyboy » Mon Mar 01, 2010 8:12 pm

I canonly think of two brackets {{0,1,2}} but that didn't work either....I clearly miss a minor but important detail :(
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: struct within struct

Postby Nyh » Mon Mar 01, 2010 8:47 pm

simonsunnyboy wrote:I canonly think of two brackets {{0,1,2}} but that didn't work either....I clearly miss a minor but important detail :(

Yes you are.

It is all very simple... :-)

Init a struct:

Code: Select all

bar={element0,element1,element2};


If one of these elements happens to be an array (element1 in this example) then you have to initialize this element linke this:

Code: Select all

bar0={element0,{element1_0, element1_1, element_1_2},element2};


If one of these elements is again an array (element1_2 in this example) the initialization just repeats itself:

Code: Select all

bar0={element0,{element1_0, element1_1, {element1_2_0, element1_2_1, element1_2_2}},element2};


Now, what do you have?

Hans Wessels

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4867
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: struct within struct

Postby simonsunnyboy » Mon Mar 01, 2010 8:59 pm

Then i am where it didn't work either:

Code: Select all

x = { { { 0,1,2,3,4} }, { { 0,2,3 } }, ... };
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: struct within struct

Postby Nyh » Mon Mar 01, 2010 9:44 pm

simonsunnyboy wrote:Then i am where it didn't work either:

Code: Select all

x = { { { 0,1,2,3,4} }, { { 0,2,3 } }, ... };

Yes this does not work.... Because you are saying that:
{element0, element1, ...}
with element0: {element0_0} and element1: {element1_0}
with element0_0:{element0_0_0, element0_0_1, element_0_0_2...}
The struct for this looks like this:

Code: Select all

struct
{
  int a[1][5];
  int b[1][3];
  ...
};


But then again you have an struct with one element which is and array and every element in the array is an array..

So...

Hans Wessels

!cube
Atari maniac
Atari maniac
Posts: 79
Joined: Thu Jun 14, 2007 6:37 am
Location: Vantaa, Finland
Contact:

Re: struct within struct

Postby !cube » Mon Mar 01, 2010 10:40 pm

In your initializer list, you're treating the variable fade_FADE_H as an array instead of a struct.

So instead of:

Code: Select all

FadeSequence fade_FADE_H = { {a, b, c}, {d, e, f} };

which would be trying to initialize an ARRAY of two elements where each element is an array of three elements, you need to use an additional pair of braces in order to initialize the first member of a STRUCT where the first member is an array of two elements where each element is an array of three elements:

Code: Select all

FadeSequence fade_FADE_H = { { {a, b, c }, {d, e, f} } };
Kludge power since 1976.

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4867
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: struct within struct

Postby simonsunnyboy » Tue Mar 02, 2010 5:31 pm

Ahhhhhhhh ... thanks! Ofcourse this now makes 100% sense with what Nyh said above. Big cheers to all of you :)
And it works ofcourse with no warnings ;)

Another lesson learned for C :D
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org


Social Media

     

Return to “C / PASCAL etc.”

Who is online

Users browsing this forum: No registered users and 1 guest