struct within struct and pointers

jgrouse: So this is not the best place to gain information on c++ programming from more experienced c++ programmers such as yourself?

I, and others here, try to be helpful, as we have been in this thread. Occasionally we come across posts asking how to "fix" code like this:

5 = a
6 = b
if a + b = 7 
  foo = bar

I'm not suggesting your code looks like that, but this is taking us back to basic language knowledge.

I stated in reply #11:

You can't assign like that. That syntax is for declarations.

Then in reply #13 you just came up with a variant of the same thing.

My code fragment above allows him to get very close to what he's posted.

Oh yeah, that's pretty cool. Looks like this forum is the place to get help after all, eh? :)

Hi guys,

thanks for the input. Part of the title of this particular forum is "understanding the language" and your input is certainly helping me in that respect. If you don't think this is the place to "understand the language" then please ask the moderators to change the title.

I do actually have most of the fundamentals of c++ squared away apart from the most foreign concept of memory pointers - foreign in the sense of being a VB programmer where I couldn't actually give a rats where .NET puts the bytes into memory, as long as I can retrieve it when I need to.

I don't expect any in depth explanation here of how wonderful that concept is in the c++ world as I'm sure there are many fine documents on the web that do just that (read one last night). What is of the most immediate help is seeing example of it's use in code snippets. lloyddean has done a fine job of that in his state of sickly insomnia (poor chap).

Nick, I am grateful for your time, however, if I was struggling with basic language concepts please explain how I could have possibly already created code that delivers functions of an advanced radio control transmitter with 8 channel PPM out, 8 model settings, full expo, trim, travel, mixing (still working on that) and a fancy menu system to alter all settings. All communicating via I2C to another Arduino embedded in a PC joystick with 10 button decoding and a 12bit I2C ADC for added resolution for the joystick pots? This was not copied code. I completely wrote the code from scratch, including a new LCD03_I2C libraray. None of the existing libraries on the web for menu's that I saw were capable of an action to scroll up or down to other menu pages or let you display dynamic variables, all of which you can modify, be it text, int or float. With this complexity, RAM has come at a steep price and I have had to move up to running the code on a Mega2560 from a Nano. Link to my blog - http://www.diydrones.com/profiles/blogs/pc-joystick-to-8ch-tx-conversion

If anyone is interested I will put the code up on Google Code hosting.

To Ikazone, I understand that the struct size cannot change, however I would have thought that an array of structs could change its bounds. Hence the sub-brackets to group each struct. Assigning the values for each index position in the array individually is what is working for me now and lloyddean's example still works with a fixed array size (MAX_DYNAMIC = 10), which is what I have set already so no real RAM saving going on there.

I've ordered a cheap SD card module and I'm going to write a Windows app to let you visually design the menu's and store each of the menu settings in binary files on the SD card. The Arduino code will then read the selected menu settings from the SD card as they are activated. This will not change the RAM used as the menu object with all its settings will not change, just the code will change to read the object structure from the files on the SD card, rather than having the definitions for each in code. If I am going with the SD card concept then having fixed array sizes will actually make reading the settings in much easier.

Your project looks interesting. Since you have a Mega you have a bit of RAM and program memory in hand, so you might want to explore the pleasures of the STL (standard template library).

It really does simplify a lot of stuff like adding things to lists, vectors, etc. and also has its own string class.

Maybe it’s overkill for a radio transmitter, but think of the fun you’ll have exploring it!

I have some tutorials about using STL on my forum:

http://www.gammon.com.au/forum/?id=2891

(plus other posts in the same section).

Cheers Nick!

Had a read of your posting suggestions sticky in this forum this morning and I can see where you are coming from. Thanks for the help.

I like the STL - at least on a full blown desktop system.

On something with as little memory as the Arduino Mega my feeling is that depending upon the project and the length of up time required it may not be a good fit.

There are other Arduino software (near) compatible devices I'd feel more comfortable with. You know, faster processor and more memory with similar pricing and development IDE.

Gives much more breathing room both speed wise and available RAM.

Arduino - Mega

Microcontroller ATmega1280 Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limits) 6-20V Digital I/O Pins 54 (of which 15 provide PWM output) Analog Input Pins 16 DC Current per I/O Pin 40 mA DC Current for 3.3V Pin 50 mA Flash Memory 128 KB of which 4 KB used by bootloader SRAM 8 KB EEPROM 4 KB Clock Speed 16 MHz

Digilent - chipKIT Max32

Microchip® PIC32MX795F512 processor 80 Mhz 32-bit MIPS 512K Flash, 128K RAM USB 2.0 OTG controller 10/100 Ethernet MAC Dual CAN controllers Provides additional memory and advanced communications peripherals Compatible with many existing Arduino code examples, reference materials and other resources Arduino™ "Mega" form factor Compatible with many Arduino™ shields 83 available I/O User LED Connects to a PC using a USB A -> mini B cable (not included)

He's got a Mega, see reply above.

[quote author=Nick Gammon link=topic=157618.msg1182048#msg1182048 date=1364873720] He's got a Mega, see reply above. [/quote]

Yes I saw that.

You can change the size of the array... it just has to be a pointer instead of an array.

I'm not 100% but I think the following code is safe.

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

struct B {
  int test;
  const A *a;
};

void setup() {
  B b;
  b.test = 1;
  // I'm not sure if this is safe but I think it is.
  b.a = (const A[]) {
    {1, 2, 3},
    {1, 3, 4}
  };
  
  // or... 
  B c = { 
    2,
    (const A[]) {
      {1, 3, 4},
      {3, 3, 1},
      {1, 2, 3}
    }
  };
}

void loop() {
}

No, that code is not safe; your use of temporaries is wrong.

To see the full effect of your mistake, add a destructor to struct A:

  ~A(){
     a = 0; 
     b = 0;
     c = 0;
  }

This test case shows how temporaries are destroyed at the end of the expression that created it. You simply create a bad pointer.

struct A {
  int a;
  int b;
  int c;
  
  ~A(){
     a = 0; 
     b = 0;
     c = 0;
  }  
};

struct B {
  int test;
  const A *a;
};

void printbytes( uint8_t *u, int len ){
 while( len-- ) Serial.print( *u++, HEX ); 
}

void setup() {
  Serial.begin(9600);
  
  B b;
  b.test = 1;
  // I'm not sure if this is safe but I think it is.
  b.a = (const A[]) {
    {1, 2, 3},
    {1, 3, 4}
  };
  
  // or... 
  B c = { 
    2,
    (const A[]) {
      {1, 3, 4},
      {3, 3, 1},
      {1, 2, 3}
    }
  };
  
  Serial.print( "Data b: " );
  printbytes( ( uint8_t* ) b.a, 12 );
  Serial.print( "\r\nData c: " );
  printbytes( ( uint8_t* )c.a, 18 );
}

void loop() {
}