Multiple arguement in a 1 dimensional array

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Folks,

I'm trying to understand some code. This is the LCD menu example program from the winavr avr-gcc tutorial. You can find that tutorial here:
http://winavr.scienceprog.com/ex...

The code I'm having trouble understanding is:

//SubMenu and Function table pointer update
uint8_t MFIndex(uint8_t, uint8_t);
SUBMENU[MFIndex(MN.menuNo, MN.subMenuNo)]

I believe the first snipet is a typecast? SUBMENU is a 1-dimensional array. When two values (MN.menuNo and MN.subMenuNo) are passed to the array in this way, what happens?

In other words: What will MFIndex actually translate to in this usage?

Thanks for the help... seems like this should be easy to understand but I'm just not seeing it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//SubMenu and Function table pointer update
uint8_t MFIndex(uint8_t, uint8_t);

This is just a function declaration. the uint8_t is just a shorthand for unsigned char (it is in fact a typedef that was probably declared in a system header file)

SUBMENU[MFIndex(MN.menuNo, MN.subMenuNo)]

The array SUBMENU[ ] just gets its index from the MFIndex() function. (that has probably returned an int)

For tutorial code, it does seem a little advanced.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah ha! That's a prototype....but not organized with the rest of the prototypes. No wonder I couldn't figure out what the heck it was doing.

Here's the accompanying function.

uint8_t MFIndex(uint8_t mn, uint8_t sb)
{
	uint8_t p=0;//points to menu in table of function pointer 
	for(uint8_t i=0; i<(mn-1); i++)
	{
		p=p+pgm_read_byte(&MSTR2[i+1]);
	}
	p=p+sb-1;
	return p;
}

Thanks for the help, mental block on my end.