0

The question is related to my former question here: Arduino compile error while using reference to a struct in another struct

I ported the sample code back to the pc and compiled it to get a clue, what's going wrong.

Here's the sample:

#include <stdio.h>

unsigned int steps=64;
unsigned int mode=0;
unsigned int speed=1;


typedef struct{
  unsigned int option_value;
  char option_name[17];
} SELECTION;

typedef struct{
  char item_name[17];
  unsigned int* variable;
  SELECTION** options;
} MENU_ITEM;


SELECTION mode_forward = { 0, "Forward" };
SELECTION mode_backward = { 1, "Backward" };
SELECTION* options_mode[] = { &mode_forward, &mode_backward };

SELECTION speed_slow = { 0, "Slow" };
SELECTION speed_normal = { 1, "Normal" };
SELECTION speed_fast = { 2, "Fast" };
SELECTION* options_speed[] = { &speed_slow, &speed_normal, &speed_fast };

MENU_ITEM menu_steps = { "Steps", &steps, NULL }; 
MENU_ITEM menu_mode = { "Mode", &mode, options_mode }; 
MENU_ITEM menu_speed = { "Speed", &speed, options_speed }; 
MENU_ITEM menu_exit = { "Exit", NULL, NULL }; 

const unsigned char menu_items = 4;
MENU_ITEM* menu_list[menu_items] = { &menu_steps, &menu_mode, &menu_speed, &menu_exit };
//-----------------------------------------------------------

int main(){

  int options;

  options=(int)(sizeof(options_speed)/sizeof(options_speed[0]));
  printf("Speed options: %i\n\n",options);

  printf("Address of speed_slow:  %p\n",&speed_slow);
  printf("Address of speed_normal:  %p\n",&speed_normal);
  printf("Address of speed_fast:  %p\n",&speed_fast);
  printf("Address of array:  %p\n\n",&options_speed);

  MENU_ITEM item;
  item=*menu_list[2];

  printf("Menu Item: %s - Item Value: %i\n",item.item_name,*item.variable);
  printf("Address of name: %p\n",&item.item_name);
  printf("Address of variable-pointer: %p\n",&item.variable);
  printf("Address of options-pointer: %p\n",&item.options);
  printf("Value of options-pointer: %p\n",*item.options);


  return 0;
}

When i start the program i get the following output:

Speed options: 3

Address of speed_slow:  0x6010c0
Address of speed_normal:  0x6010e0
Address of speed_fast:  0x601100
Address of array:  0x601120

Menu Item: Speed - Item Value: 1
Address of name: 0x7fff18a5dc90
Address of variable-pointer: 0x7fff18a5dca8
Address of options-pointer: 0x7fff18a5dcb0
Value of options-pointer: 0x6010c0

That's what i expect....except the last line. Shouldn't the address that it points to be 0x601120 - the address of options_speed array? Why does it point to the first member of the array instead? What do i have to change to let it point to 0x601120?

3
  • 1
    Verify the operator precedence of *menu_list[2] is doing what you think it is. Commented Feb 11, 2015 at 23:03
  • *menu_list[2] and *(menu_list[2]) give me the same result, so i assume that part to be right...but i'll add the parentheseis, just to be sure. Commented Feb 11, 2015 at 23:16
  • It would definitely clarify intent, though you're correct, I believe. I also think 6502 is accurate in his answer. see it live Commented Feb 11, 2015 at 23:24

1 Answer 1

1

You are evaluating *item.options, not item.options. This seems is not what you wanted to print (i.e. the "options pointer") as there is an extra dereferencing operation.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah...it's a step forward. If a change the line from printf("Value of options-pointer: %p\n",*item.options); to printf("Value of options-pointer: %p\n",item.options); i'm getting the right result....but now i stuck on the next step. I'll play around a bit, before coming back to that question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.