Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How come C allow structure members to be addressed like an array ?

Reply
Thread Tools

How come C allow structure members to be addressed like an array ?

 
 
pereges
Guest
Posts: n/a
 
      06-14-2008
#include <stdio.h>

typedef struct
{
double x, y, z;
}vector;

int main(void)
{
int i;
vector v;
double *cord;

v.x = 10;
v.y = 1;
v.z = 2;

cord = &v.x;

for(i = 0; i < 3; i++)
{
printf("%f\n", cord[i]);
}
return 0;
}

here's the output i get:
10.000000
1.000000
2.000000

which is the same as v

i don't how it happens as i was just trying some random ideas but
great stuff really. helped me to reduce some of my code to almost
1/3rd its size.
 
Reply With Quote
 
 
 
 
EventHelix.com
Guest
Posts: n/a
 
      06-14-2008
On Jun 14, 5:37*pm, pereges <(E-Mail Removed)> wrote:
> #include <stdio.h>
>
> typedef struct
> {
> * double x, y, z;
>
> }vector;
>
> int main(void)
> {
> * int i;
> * vector v;
> * double *cord;
>
> * v.x = 10;
> * v.y = 1;
> * v.z = 2;
>
> *cord = &v.x;
>
> *for(i = 0; i < 3; i++)
> *{
> * printf("%f\n", cord[i]);
> *}
> return 0;
>
> }
>
> here's the output i get:
> 10.000000
> 1.000000
> 2.000000
>
> which is the same as v
>
> i don't how it happens as i was just trying some random ideas but
> great stuff really. helped me to reduce some of my code to almost
> 1/3rd its size.


This is not portable but at the low level there is no difference
between a structure containing three doubles and an array of three
doubles in this case.

You cannot assume this, as some implementation might introduce pads
for byte alignment in structures.

The following articles should help:

http://www.eventhelix.com/RealTimeMa...ranslation.htm
http://www.eventhelix.com/RealTimeMa...anslation2.htm
http://www.eventhelix.com/RealTimeMa...anslation3.htm

--
EventStudio 4.0 - http://www.Eventhelix.com/Eventstudio/
Sequence diagram based systems engineering tool
 
Reply With Quote
 
 
 
 
Thomas Lumley
Guest
Posts: n/a
 
      06-14-2008
On Jun 14, 2:37 pm, pereges <(E-Mail Removed)> wrote:
> #include <stdio.h>
>
> typedef struct
> {
> double x, y, z;
>
> }vector;


The elements of a structure are laid out in memory in the order
they appear in the structure. In principle there could be empty
space between the elements (padding), but it is not likely when
all the elements have the same type -- a contiguous layout must
be possible in that case, and is likely to be efficient, since
it is what an array would use.

Still, the Standard allows padding and does not require
implementations to be efficient, sensible, or cooperative with
the user.

> int main(void)
> {
> int i;
> vector v;
> double *cord;
>
> v.x = 10;
> v.y = 1;
> v.z = 2;
>
> cord = &v.x;


This is legitimate, since v.x is a double

> for(i = 0; i < 3; i++)
> {
> printf("%f\n", cord[i]);


This obviously has no chance of working unless all the
structure members are double. Even if they are, there
could (in theory) be padding between them, in which case
cord+1 wouldn't point to v.y. Even if it does, I think
that taking a pointer to v.x, incrementing it so it points
beyond the end of v.x, and then dereferencing it, has
undefined behaviour.


> }
> return 0;
>
> }
>



-thomas
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-14-2008
pereges <(E-Mail Removed)> writes:
> #include <stdio.h>
>
> typedef struct
> {
> double x, y, z;
> }vector;
>
> int main(void)
> {
> int i;
> vector v;
> double *cord;
>
> v.x = 10;
> v.y = 1;
> v.z = 2;
>
> cord = &v.x;
>
> for(i = 0; i < 3; i++)
> {
> printf("%f\n", cord[i]);
> }
> return 0;
> }
>
> here's the output i get:
> 10.000000
> 1.000000
> 2.000000
>
> which is the same as v
>
> i don't how it happens as i was just trying some random ideas but
> great stuff really. helped me to reduce some of my code to almost
> 1/3rd its size.


C doesn't really allow this. More specifically, it lets you get away
with trying it (the compiler isn't required to issue a diagnostic),
but provides absolutely no guarantee that it will work. Compilers are
allowed substantial freedom in how they lay out members of structures.
The first member must be at offset 0, and they must be allocated in
declared order, but the compiler is allowed to insert padding between
members and/or after the last one.

Usually (practically always), such padding is inserted to satisfy
alignment requirements. For a structure containing multiple members
all of the same type, it's very unlikely that any such padding would
be required. In other words, you're not likely to run across an
implementation where your code doesn't happen to work.

Which means that you have a bug that's very difficult to track down
because the symptom rarely if ever appears.

If treating your vector as an array makes your code 1/3 its previous
size, why not just declare it as an array in the first place? It
might be convenient to wrap the array in a structure. For example:

typedef struct {
double v[3];
} vector;

(I'd declare it as "struct vector" and drop the typedef, but that's a
matter of taste.)

If you insist, you could even define macros that let you refer to the
members by their original names:

#define x v[0]
#define y v[1]
#define z v[2]

though this borders on obfuscation.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-14-2008
In article <(E-Mail Removed)>,
EventHelix.com <(E-Mail Removed)> wrote:

>This is not portable but at the low level there is no difference
>between a structure containing three doubles and an array of three
>doubles in this case.


Fields in a struct are aligned in "an implementation-defined manner
appropriate to [their] type". The clear implication is that the
padding is a property of the type (rather than each individual
instance) and so it would be unreasonable for the padding of
successive members of the same type in a struct to be different from
that in an array - which is to say, there should be no padding between
fields of the same type.

Can anyone come up with a reason (other than perverseness) why an
implementation would not do this?

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
 
Reply With Quote
 
pereges
Guest
Posts: n/a
 
      06-14-2008
one can solve this problem using a vector iterator function like below
but i am not sure if the solution is generic :

#include <stdio.h>

typedef struct
{
double x, y, z;

}vector;

double vector_iterator(vector *a, int i)
{
double ret;
if(i == 0)
ret = a->x;
if(i == 1)
ret = a->y;
if(i == 2)
ret = a->z;
return ret;
}

int main(void)
{
int i;
vector v;
double coord;

v.x = 10;
v.y = 1;
v.z = 2;

for(i = 0; i < 3; i++)
{
coord = vector_iterator(&v, i);
printf("%f\n", coord);
}
return 0;

}

 
Reply With Quote
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      06-14-2008

Others have explained the padding situation, but if you want re-
assurance, you might add a routine to the start of your code that
makes sure it's OK:

void EnsureItWorks(void)
{
Vector const v = { 99, 56, 42 };

if (99 == (&v.x)[0] &&
56 == (&v.x)[1] &&
42 == (&v.x)[2]) return;

puts("ERROR: There's padding in the Vector struct.");

exit(EXIT_FAILURE);
}

int main(void) { EnsureItWorks(); }


(I know that floating-point arithmetic isn't exact but as far as I
know it's exact for integer values... but I'm open to correction!)
 
Reply With Quote
 
badc0de4@gmail.com
Guest
Posts: n/a
 
      06-14-2008
Tomás Ó hÉilidhe wrote:
> void EnsureItWorks(void)
> {
> Vector const v = { 99, 56, 42 };
>
> if (99 == (&v.x)[0] &&
> 56 == (&v.x)[1] &&
> 42 == (&v.x)[2]) return;


*BANG*
(&v.x)[1] or (&v.x)[2] is a trap representation

> puts("ERROR: There's padding in the Vector struct.");
>
> exit(EXIT_FAILURE);
> }

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-14-2008
Tomás Ó hÉilidhe wrote:
> Others have explained the padding situation, but if you want re-
> assurance, you might add a routine to the start of your code that
> makes sure it's OK:
>
> void EnsureItWorks(void)
> {
> Vector const v = { 99, 56, 42 };
>
> if (99 == (&v.x)[0] &&
> 56 == (&v.x)[1] &&
> 42 == (&v.x)[2]) return;
>
> puts("ERROR: There's padding in the Vector struct.");
>
> exit(EXIT_FAILURE);
> }
>
> int main(void) { EnsureItWorks(); }
>

A very verbose way of writing

int main()
{
assert( sizeof(vector)==sizeof(double)*3);
}

--
Ian Collins.
 
Reply With Quote
 
viza
Guest
Posts: n/a
 
      06-15-2008
Hi

On Sun, 15 Jun 2008 10:51:28 +1200, Ian Collins wrote:
> Tomás Ó hÉilidhe wrote:


>> if (99 == (&v.x)[0] &&
>> 56 == (&v.x)[1] &&
>> 42 == (&v.x)[2]) return;
>> puts("ERROR: There's padding in the Vector struct.");
>> exit(EXIT_FAILURE);


> A very verbose way of writing
> assert( sizeof(vector)==sizeof(double)*3);


The two are not equivalent and neither is perfect. Better is:

assert( offsetof(vector,y) == sizeof(double)
&& offsetof(vector,z) == sizeof(double)*2 );

viza
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Lowest addressed character in array arnuld C Programming 8 12-16-2010 08:40 PM
CNE question? 127 loopback addressed murrayatuptowngallery@yahoo.com Computer Security 4 02-19-2006 07:02 PM
Critique CSS layout (issues addressed) lime HTML 14 11-17-2004 08:08 PM
I am getting loads of spam by e-mail.Most of it is not even addressed to me so god knows how I am receiving it. Any ideas . Is there a spam guard available on blueyonder? How do I get it on my system if there is?Cheers GW Geoff/Elaine Computer Support 11 11-16-2004 11:17 PM



Advertisments