Re: gets() and "string" variable type
- Posted by Matt Lewis <matthewwalkerlewis at g?ail.c?m> Sep 15, 2007
- 581 views
Pete Lomax wrote: > > PS [OT] Can someone explain the last line of this snippet from be_execute.e: > > top = (object)*(top + ((s1_ptr)obj_ptr)->base); > > My grasp of C is limited and I do not actually understand how it knows to get > "top" from "base+top*4" rather than just "base+top". Not that it does not seem > logical, more "how does it know?" Specifically, which parts of > > typedef long object; > typedef object *object_ptr; > > are applied/applicable when and where? Is it the earlier *(object_ptr) or the > latter (object)* or both or what? The type defs just tell the compiler, "When I say 'object' please read it as 'long', and when I say 'object_ptr' please read it as 'long*'." The expression you put from be_execute.c is what's often referred to as pointer arithmetic. When you add 1 to a pointer, it points to the next place in memory, *after the current value*. This applies for structs, too, BTW. Another way to have written this would be: top = ((s1_ptr)obj_ptr)->base[top] Which, to me, is usually clearer (and how I tend to code that sort of thing, so when you see it in the code, it's probably something I wrote). Matt