ROBOTC.net forums
http://robotc.net/forums/

question about stacks and structs
http://robotc.net/forums/viewtopic.php?f=1&t=10910
Page 1 of 1

Author:  beefy1875 [ Mon Apr 13, 2015 8:41 pm ]
Post subject:  question about stacks and structs

Hi,

I have been trying to make sense of the tutorial over on Xanders botbench.com - http://botbench.com/blog/2013/01/20/tut ... in-robotc/.

In the example, he sets up an individual stack element struct to store individual values, and then sets up a stack struct to hold the elements (individual values). Why is this done? Would it not be possible (and easier) to simply store the individual elements directly in the stack struct?

I've been trying to work this out for quite some time now, so any help would be much appreciated.

Thanks.

Author:  MHTS [ Tue Apr 14, 2015 3:33 am ]
Post subject:  Re: question about stacks and structs

Since I am not Xander, I cannot tell you why he did it that way but I can guess. I think this is an example to show you how stacks work. The stack struct is to make it a stack object so you can pass it around by a pointer to it. You could define an array of int as the element of the stack struct directly but declaring the stack element as another struct is to just show you the element can be anything, not just int. Therefore, if you want to declare more fields in the stack element, you could. Also, it makes the functions that manipulate the stack element agnostic to what the type of the element is. So it means the push and pop functions can be prototyped as:
Code:
void push(tStack *stack, tStackElement *element);
tStackElement *pop(tStack *stack);

But then when I looked at Xander's push and pop functions, they are pushing and popping int. So that defeats my explanation. In other words, I don't know why except that it's just an example for demonstration purpose.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/