| Date: | Thu, 13 Apr 2006 10:03:44 +0300 |
| From: | Diomidis Spinellis <dds@aueb.gr> |
| Organization: | Athens University of Economics and Business |
| User-Agent: | Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0 |
| MIME-Version: | 1.0 |
| Newsgroups: | comp.lang.c |
| Subject: | Re: A doubt about struct |
| References: | <1144906769.377398.21630@u72g2000cwu.googlegroups.com> |
| In-Reply-To: | <1144906769.377398.21630@u72g2000cwu.googlegroups.com> |
| Content-Type: | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding: | 7bit |
Alex wrote:
> If I have two struct. See below:
> struct s1
> {
> int type;
> int (*destroy)(struct s1* p);
> }
>
> struct s2
> {
> struct s1 base;
> .../* here are some other memebers */
> }.
>
> My doubt is that Will the standard C will guarantee the cast from
> 'struct s2' to 'struct s1' is safe. The problem comes because I don't
> know
> whether the align in struct will affect such cast.
You can only cast scalar types, not composite types like structures, so
I assume that you mean a cast from (struct s2 *) to (struct s1 *). The
C99 standard (6.7.2.1) specifies that a pointer to a structure object,
suitably converted, points to its initial member. Therefore, your
proposed cast is legal and guaranteed to work.
--
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality
Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.