Em 16/4/2010 11:22, Byron Jeff escreveu: > On Thu, Apr 15, 2010 at 11:23:21PM -0400, Justin Richards wrote: > = >>> it. For example I would have declared the stuct above as: >>> >>> typedef struct Temp >>> { >>> unsigned char Var00; >>> unsigned char Var01; >>> unsigned char Var02; >>> unsigned char *Var03; >>> } Temp_t, *Temp_p; >>> >>> This creates two new types named Temp_t, which is the structure, and >>> Temp_p, which is a pointer to that type of structure. From then on you = can >>> use those names getting rid of that horrid struct keyword. For example >>> instead of: >>> >>> Temp_t new1, new2; // Much cleaner. >>> >>> Seeking more clarification here. >>> = >> I get the pointer thing to some degree. Passing address to variables ra= ther >> than passing the values of the variables. And a pointer contains the >> address of a variable and I can see that Temp_t is of type struct as def= ined >> above but what is *Temp_p. >> = > Defined as a pointer to the struct. Useful when you want to pass the stru= ct > around in a function and change its contents. For example. > > void new_var00(Temp_p target,unsigned char testval) > { > target->Var00 =3D testval; > } > > = >> I assume *Temp_p does not set aside or allocate memory to which it point= s to >> or does it. >> = > It does not. There's another trick I use for that. Arrays in C allocate > space and the name represents a pointer to that space. So while lost folks > who wanted to use the new_var00 function above would do something like: > > void main() > { > Temp_t mine; > > new_var00(&mine,14); > } > > Having to do the address of operator always drove me nuts. So instead I > would do the following: > > void main() > { > Temp_t fixed[1]; > > new_var00(fixed,18); > } > > Allocating an array with one element gives the space for a single structu= re > but the much more convenient handle of a pointer to the structure for the > name. > > = >> If it is a pointer to that type of structure when do you use it, inside >> another structure perhaps?. >> = > See above. All of it is just simplifying the syntax around the restrictio= ns > that C puts upon you. = > > = >> To delare a pointer to the start of a memory >> location with that type of structure. >> = > You can do that too of course if you want to dynamically allocate > structures. But as you can see, even if you are only using it for paramet= er > management, declaring a pointer to the struct is still a good thing to do= . = > > My real objective is to never have to use the struct keyword anywhere els= e in > the program other than in the typedef delcaration. To accomplish that, a > name has to be attached to the struct pointer type in addition to the > struct type even if the context is only for parameter passing in function= s. > > The vast majority of my programs never use the dot operator for structs. > Virtually every structs name is attached to a pointer and the arrow > operator (->) is my best friend. > > = >> Like >> >> Temp_p a_new_pointer; >> >> a_new_pointer =3D &new1; >> = > Presuming that new1 is a struct. But to reiterate you can get both space > and a pointer to that space simply be declaring a single element array. > i.e. > > Temp_t new1[1]; > > Now new1 is a pointer to the allocated first element. No need to track two > different names. > = You just replaced one annoyance by another, now you need to use "new1[0].Var00" and so on. Isaac __________________________________________________ Fale com seus amigos de gra=E7a com o novo Yahoo! Messenger = http://br.messenger.yahoo.com/ = -- = http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist