View unanswered posts | View active topics It is currently Sat Sep 19, 2020 11:16 am

Reply to topic  [ 2 posts ] 
question about stacks and structs 
Author Message

Joined: Tue Sep 23, 2014 5:15 am
Posts: 7
Post question about stacks and structs

I have been trying to make sense of the tutorial over on Xanders - ... 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.


Mon Apr 13, 2015 8:41 pm
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post 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:
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.

Tue Apr 14, 2015 3:33 am
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.