PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Struct/Union pointer stuff
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Struct/Union pointer stuff

Réponse
 
LinkBack Outils de la discussion
Vieux 14/04/2008, 06h29   #1
Kenneth Bull
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Struct/Union pointer stuff



Say,

struct foo
{
int x;
double y;
/* etc. more variables defined */
short u;
char z;
} a;


Are the following true on all systems?

1) &a == &a.x
2) (&a.x < &a.y) && ( .. etc.. ) && ( &a.u < & a.z)


Are the following true if we change 'struct' to 'union' above, on all
systems?

3) &a == &a.x
4) &a.? == &a.? where ?s can be x, y, ..., u, or z (not necessarily
equal for both ?s)


Thank you for the .





  Réponse avec citation
Vieux 14/04/2008, 06h53   #2
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Struct/Union pointer stuff

Kenneth Bull wrote:
>
> struct foo {
> int x;
> double y;
> /* etc. more variables defined */
> short u;
> char z;
> } a;
>
> Are the following true on all systems?
>
> 1) &a == &a.x
> 2) (&a.x < &a.y) && ( .. etc.. ) && ( &a.u < & a.z)
>
> Are the following true if we change 'struct' to 'union' above, on all
> systems?
>
> 3) &a == &a.x
> 4) &a.? == &a.? where ?s can be x, y, ..., u, or z (not necessarily
> equal for both ?s)


Yes to all.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 14/04/2008, 07h01   #3
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Struct/Union pointer stuff

Kenneth Bull wrote:
> Say,
>
> struct foo
> {
> int x;
> double y;
> /* etc. more variables defined */
> short u;
> char z;
> } a;
>
>
> Are the following true on all systems?
>
> 1) &a == &a.x
> 2) (&a.x < &a.y) && ( .. etc.. ) && ( &a.u < & a.z)


No, but you're almost there.

Note that the pointer types don't match. Your expressions
violate constraints. It is true to say that...

(int *) &a == &a.x
&a == (struct foo *) &a.x

offsetof(struct foo, x) == 0
offsetof(struct foo, x) < offsetof(struct foo, y)
offsetof(struct foo, y) < offsetof(struct foo, u)
offsetof(struct foo, u) < offsetof(struct foo, z)

> Are the following true if we change 'struct' to 'union' above, on all
> systems?
>
> 3) &a == &a.x


(int *) &a == &a.x
&a == (union foo *) &a.x

> 4) &a.? == &a.? where ?s can be x, y, ..., u, or z (not necessarily
> equal for both ?s)


If the addresses are converted to void * or suitable type, they must
compare equal.

Note that it is not possible to take the address of bitfields.

--
Peter
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13853 seconds with 11 queries