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

B

Ben Bacarisse

Ian Collins said:
Ben said:
Ian Collins said:
viza wrote:
Hi

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 );

Why?

Tomás's code is over complex (one test is enough) and could fail in
unusual cases (see my other posting) and your test will be thrown if
there is padding at the end of the struct.
OK, make it

assert( sizeof(vector[2])==sizeof(double)*6 );

Or even a compile time assert:

const int n = 1/( sizeof(vector[2])==sizeof(double)*6 );

I don't think any of the proposed sizeof tests work due to possible
padding at the end of the struct. I know this has been covered by
later messages, but it seems worth summarising.
Fair enough, but the simplified offsetof (or sizeof) test has the
(small) advantage of not requiring an instance of vector to test.

Good point. That gives the offsetof version the edge, I'd say.
 
A

Antoninus Twink

Vector const v = { 99, 56, 42 };

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

Holy crap, are you trying to follow the CBF anti-style rules?
(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!)

It's nothing to do with integer values. Just passing around a
floating-point value won't magically change its bits.

In

double a=42;
double b=42.422242;
double c=1./3;
assert(a == 42);
assert(b == 42.422242);
assert(c == 1./3);

all the asserts will succeed.

Problems only arise when you start doing calculations and get
incremental rounding errors, either from separate steps in the
calculation, or from single steps where the compiled code uses a
temporary, or from type conversions (e.g. float promoted to double).
 
A

Antoninus Twink

Thomas: Please change your name to ASCII.

Probably he cares more about making a nationalist point than making life
simpler for people using non-unicode aware terminals/newsreaders. It's
interesting that most Russian, Chinese and Japanese posters are able to
swallow their pride enough to Latinize their names.
 
B

Ben Bacarisse

Antoninus Twink said:
Probably he cares more about making a nationalist point than making life
simpler for people using non-unicode aware terminals/newsreaders. It's
interesting that most Russian, Chinese and Japanese posters are able to
swallow their pride enough to Latinize their names.

Well, that helps me get a clearer picture of how you see the world.

I think it is quite reasonable that people should be able to spell
their names correctly, but that argument will be lost on someone who
does not even dare let their name be know.
 
J

Joachim Schmitz

Ben said:
Well, that helps me get a clearer picture of how you see the world.

I think it is quite reasonable that people should be able to spell
their names correctly, but that argument will be lost on someone who
does not even dare let their name be know.
Just curious: what makes you believe that Antonius Twink is not a real name?

There are quite a few 'regulars' here that obviously don't use real names,
Default User for example and quite a few where it's hard to tell whether
it's a nick, a first or a last name. To me Antonius Twink /apperas/ to be a
real name.

Bye, Jojo
 
J

Joachim Schmitz

Antoninus said:
Vector const v = { 99, 56, 42 };

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

Holy crap, are you trying to follow the CBF anti-style rules?
(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!)

It's nothing to do with integer values. Just passing around a
floating-point value won't magically change its bits.

In

double a=42;
double b=42.422242;
double c=1./3;
assert(a == 42);
assert(b == 42.422242);
assert(c == 1./3);

all the asserts will succeed.
I think you're missing the point. Tomás wantes to check whether it is safe
to assume that struct {double a;double; double c;} can be interpreted as
double[3], i.e. whether there are any padding bytes in the struct that would
prevent that.
And it seems on anything but a DS9K it is save to assume that no padding
bytes are between the members (of equal typr), but it is very well possible
that there are some after the last member.

Bye, Jojo
Bye, Jojo
 
S

santosh

Joachim said:
Just curious: what makes you believe that Antonius Twink is not a real
name?

There are quite a few 'regulars' here that obviously don't use real
names, Default User for example and quite a few where it's hard to
tell whether it's a nick, a first or a last name. To me Antonius Twink
/apperas/ to be a real name.

I suppose it's the Twink part that leads people to think he is a pseudo.
Also Antoninus isn't IME a very common name either. Besides he used to
(still does?) troll clc for a long time. It's unlikely that he is
stupid enough to troll under his real name.
 
R

Richard

Ben Bacarisse said:
Well, that helps me get a clearer picture of how you see the world.

I think it is quite reasonable that people should be able to spell
their names correctly, but that argument will be lost on someone who
does not even dare let their name be know.

You appear to have totally missed the point. If you re-read Twinks reply
then all the necessary points are therein.

Hint : Most usenet posting Germans convert the German characters to
"oe", "ae" and "ss" etc as appropriate.
 
B

Bart

(e-mail address removed)...

For something more robust you can try helper functions like get() and
set(), or work with pointers using getp():

#include <stdio.h>

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

double *getp(vector *p,int i){
if (i==0) return &(p->x);
if (i==1) return &(p->y);
return &(p->z);
}

double get(vector *p,int i){
if (i==0) return p->x;
if (i==1) return p->y;
return p->z;
}

void set(vector *p,int i,double a){
if (i==0) {p->x=a; return;}
if (i==1) {p->y=a; return;}
p->z=a;
}

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

for (i=0; i<3; ++i)
set(&v,i,(i+1)*10);

for(i = 0; i < 3; i++)
printf("%f\n", get(&v,i));

return 0;
}

Where you are sure the struct is laid out like an array, the contents
of get/set/getp can be simplied to a single line (possibly inlined).
But your main code doesn't change.
 
R

Richard Tobin

Antoninus Twink said:
Probably he cares more about making a nationalist point than making life
simpler for people using non-unicode aware terminals/newsreaders. It's
interesting that most Russian, Chinese and Japanese posters are able to
swallow their pride enough to Latinize their names.

His name is perfectly Latinised. It just isn't Anglicised. Unless
you take "Latin" to be what the ancient Romans used, in which case
your own name isn't Latinised.

-- Richard
 
J

Joachim Schmitz

Richard said:
You appear to have totally missed the point. If you re-read Twinks
reply then all the necessary points are therein.

Hint : Most usenet posting Germans convert the German characters to
"oe", "ae" and "ss" etc as appropriate.
In Postings yes, but not neccessarily in Names

Bye, Jojo
 
K

Keith Thompson

Ben Bacarisse said:
Well, that helps me get a clearer picture of how you see the world.

I think it is quite reasonable that people should be able to spell
their names correctly, but that argument will be lost on someone who
does not even dare let their name be know.

Please don't feed the troll.
 
A

Antoninus Twink

I think you're missing the point. Tomás wantes to check whether it is safe
to assume that struct {double a;double; double c;} can be interpreted as
double[3], i.e. whether there are any padding bytes in the struct that would
prevent that.

Yes, I got that - I was just commenting on Thomas's seeming confustion
on how floating-point numbers behave.
 
A

Antoninus Twink

Well, that helps me get a clearer picture of how you see the world.

I passed no judgment, but merely tried to explain Thomas's likely
thinking.
I think it is quite reasonable that people should be able to spell
their names correctly, but that argument will be lost on someone who
does not even dare let their name be know.

Thomas can and should do what he likes. I very rarely read Usenet other
than in a Unicode-aware environment, but when I do it's a pain to have
the terminal screwed up by Thomas or Harold van Dijk's non-ASCII
characters. People with strange characters in their name need to decide
whether it's more important to use the "right" spelling of their name,
or to maximize the number of people who can read their messages without
a problem. I pointed out that Thomas makes a decision that's different
from many other people - I neither endorse nor condemn him for that.
 
A

Antoninus Twink

I suppose it's the Twink part that leads people to think he is a pseudo.

Someone pointed out that homosexuals have taken the perfectly good
English word (and name) Twink, and turned it into some crude slang
expression. That's unfortunate for those outside that community who
happen to already have (or use) that name. It's a bit like Gay is a
common surname, but presumably kids with that name suffer a lot in
school because of a similar hijacking of language.

In the end, what are you going to do? A name is a name is a name.
 
J

Joachim Schmitz

Antoninus said:
I passed no judgment, but merely tried to explain Thomas's likely
thinking.


Thomas can and should do what he likes. I very rarely read Usenet
other than in a Unicode-aware environment, but when I do it's a pain
to have the terminal screwed up by Thomas or Harold van Dijk's
non-ASCII characters. People with strange characters in their name
need to decide whether it's more important to use the "right"
spelling of their name, or to maximize the number of people who can
read their messages without a problem. I pointed out that Thomas
makes a decision that's different from many other people - I neither
endorse nor condemn him for that.
And you mis-transcribe/-spell them, there's no h in Tomás and no o in
Harald. And nothing in these characters is funny.

Bye, Jojo
 
T

Tomás Ó hÉilidhe

Probably he cares more about making a nationalist point than making life
simpler for people using non-unicode aware terminals/newsreaders.


Accurate observation, even for a trolling thick.

It's
interesting that most Russian, Chinese and Japanese posters are able to
swallow their pride enough to Latinize their names.


I've always hastened to use the cliché term "There's nothing worse
than X", but in this case they're the only words that come to me:
There's nothing worse than a people without pride. There's plenty of
immigrants in the town where I live. There's Eastern Europeans.
There's black Africans. While I'm not overjoyed with immigrants taking
natives' jobs and accommodation, I don't bear any real animosity or
hatred towards them. Some of them are really nice people, and they're
just here to make a better life for themselves. In fact, their
ambition in life is something a lot of people could aspire to. The
black Africans in particular tend to be extremely pleasant friendly
people; I can smile and say hello to a complete stranger black
African, and he'll say hello back with a warm smile. If I were to do
that to any other stranger, the person would look at me like I'm a
weirdo.

But then there's one immigrant people here that I truly despise: Roma
gypsies. These people have no pride whatsoever. They really are rats.
They come into my country illegally and create encapments at motorway
junctions. They teach their children how to walk in a way that makes
their bare leg look mangled, and then they send their kids to beg
between cars on the road at a busy motorway junction. They have a
mouth full of gold teeth (yes, actual gold, the rare chemical
element), and they drive BMW cars (for those who don't know, BMW is a
luxury car make), but yet they send their children to beg on the
roads. They don't wash their bodies.

Lastnight I was in a shop in my hometime where a friend of mine works.
A Roma gypsie man came up to the counter and asked if anyone could
read a German letter for him. I like to give people the benefit of the
doubt and to be open to friendship with new people, but in this case
this was very naive of me. I read the letter aloud for him. Next, as I
was leaving, he came over to me and snooped around at my car asking me
how much I'd sell it to him for. Again, being naive I plainly told him
"No sorry, I need this car to get to work, it's not for sale" while he
snooped around the car looking in my driver's door.

When I got home, I noticed something was missing. Something which I
had when I was reading the letter for him, but which was gone by the
time I got into my car.

Now up until that point, I'd never been a fan of Roma gypsies, but at
the same time I'd no real animosity or hatred for them. Now though, I
don't even see them as people. There are friends of mine who work in
shops that have had far more experience with Roma gypsies than I have,
but I've never really understood the venomous hatred my friends have
had for them. But now, I understand.

But I digress, there's nothing worse than a people without pride. I'm
Irish, and I'm from Ireland. The vast majority of people in my country
have English names, you'll see "Stephen McAteer" instead of "Stiofán
Mac an tSaoir". Now if they feel comfortable with that, then that's
fine. But personally I don't feel comfortable as an Irish person with
an English name. I'm Irish, I speak Irish and I've an Irish name.
Suits me much better.
 
C

christian.bau

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.

What you are doing invokes undefined behaviour.

As soon as you are using an optimising compiler, there are excellent
chances that your code will run into serious trouble.

For example, optimising compilers will often keep all the members of a
struct in registers; so whether a program uses your "vector" or three
doubles would make no difference. Since you take the address of v.x,
the compiler could decide to put v.x into memory, and v.y and v.z into
two floating-point registers. Your loop prints v.x plus the contents
of the memory following it, instead of v.y and v.z. This is legal and
correct under the "as if" rule.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top