Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!vrdxhq!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.unix,net.unix-wizards,net.arch,net.lang.c Subject: Re: (Really addressing inside struct)Re: get size of malloc'd object Message-ID: <484@hadron.UUCP> Date: Thu, 24-Jul-86 01:09:55 EDT Article-I.D.: hadron.484 Posted: Thu Jul 24 01:09:55 1986 Date-Received: Sun, 27-Jul-86 21:03:07 EDT References: <165@daisy.UUCP> <334@valid.UUCP> <2206@peora.UUCP> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 44 Summary: Put thereptile before the lizard, where it belongs. Xref: mnetor net.unix:5043 net.unix-wizards:7292 net.arch:2853 net.lang.c:5472 In article <403@anasazi.UUCP> john@anasazi.UUCP (John Moore) writes: >We have a structure with a number of structures contained within it. >Because of linked list processing, we have a pointer to one of the >structures within, and we want to derive a pointer to the top of >the structure without LINT complaining, and in a truly portable way. >struct links { > struct links *forward; > struct links *backward; >}; >struct foo { > char bar; > struct links snake; > struct whocares junk; > struct links lizard; >}; Assume struct foo ssssss; >struct links *reptile = &ssssss.lizard; >struct foo *iwish = (mystery function of reptile); >Example of a wrong solution: >struct foo *iwish = (struct foo *)((char *)reptile + (char *)0->lizard); You can't add pointers. There really isn't a very good and portable solution in C. Given your constraints, the best is to replace "+ (char *)0->lizard" with: "- ((int)(((char *)&ssssss.lizard) - ((char *)&ssssss)))"; and even this is not guaranteed. What you need to do is expand your mental model somewhat. Assuming that links are always inside foos (to get out of the necessity of using unions): struct links { struct foo *forward; struct foo *backward; }; Replace mystery-function with identity. Replace ->forward with ->forward->lizard or ->forward->snake, as appropriate. You may find yourself realizing a better yet way to express yourself, and say what you mean (instead of what you thought you wanted to cleverly express). -- Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP} jsdy@hadron.COM (not yet domainised)