473,804 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

#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.
Jun 27 '08
85 2448
On 16 Jun 2008 at 0:34, Keith Thompson wrote:
Stop talking about the "Roma gypsies", right now. Don't respond to
this. Don't try to defend your opinions. Just shut the hell up about
it.
However unpleasant you may find Thomas's racist views, don't you believe
even more strongly that he has a right to free speech?

Jun 27 '08 #71
Antoninus Twink wrote:
On 16 Jun 2008 at 0:34, Keith Thompson wrote:
>Stop talking about the "Roma gypsies", right now. Don't respond to
this. Don't try to defend your opinions. Just shut the hell up
about it.

However unpleasant you may find Thomas's racist views, don't you
believe even more strongly that he has a right to free speech?
Again you fail to understand that there is such a thing called
topicality. Even you must admit that the current subject is simply not
topical in clc. And it's not hair-splitting or pedanticism either. The
subject is *massively* off-topic.

Why not move it to soc.culture.iri sh or somewhere similar?

Jun 27 '08 #72
On Jun 16, 9:46 pm, santosh <santosh....@gm ail.comwrote:
troll wrote:
<troll snip>
Again you fail to understand that there is such a thing called
topicality. Even you must admit that the current subject is simply not
topical in clc. And it's not hair-splitting or pedanticism either. The
subject is *massively* off-topic.

Why not move it to soc.culture.iri sh or somewhere similar?
santosh, he does understand that. He's a troll, remember? ;-)
Let's just let this thread die peacefully... as you noted such
discussions are massively off-topic, and people can be quite
passionate about them so it's best to just let the topic die.
Jun 27 '08 #73
santosh said:
Antoninus Twink wrote:
>On 16 Jun 2008 at 0:34, Keith Thompson wrote:
>>Stop talking about the "Roma gypsies", right now. Don't respond to
this. Don't try to defend your opinions. Just shut the hell up
about it.

However unpleasant you may find Thomas's racist views, don't you
believe even more strongly that he has a right to free speech?

Again you fail to understand that there is such a thing called
topicality. Even you must admit that the current subject is simply not
topical in clc. And it's not hair-splitting or pedanticism either. The
subject is *massively* off-topic.
I agree that it's off-topic. For once I actually agree with Mr Twink, too:
Tomas has the right to peddle his revolting opinions in public if he
wishes. But we are not obliged to listen. And this is supposed to be a
techie group, not a sounding-board for racists.
Why not move it to soc.culture.iri sh or somewhere similar?
I think it would be more appropriate to move it to alt.racist.bigo t, don't
you? But of course, realistically it's way too late to move the discussion
anywhere. There is a certain inertia to a thread, and each new participant
increases that inertia, because it gets harder and harder to get everyone
to agree to subscribe to the new group and continue the thread therein.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #74
In article <sl************ *******@nospam. invalid>,
Antoninus Twink <no****@nospam. invalidwrote:
>On 16 Jun 2008 at 0:34, Keith Thompson wrote:
>Stop talking about the "Roma gypsies", right now. Don't respond to
this. Don't try to defend your opinions. Just shut the hell up about
it.
>However unpleasant you may find Thomas's racist views, don't you believe
even more strongly that he has a right to free speech?
How does telling someone not to say something infringe on their right
to free speech?

-- 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)
Jun 27 '08 #75
Tomás Ó hÉilidhe wrote:
On Jun 16, 10:52 am, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:>
>In my country (UK) you'd have problems with the law for behaving the
way you propose (and a good thing too).


Warning a Roma gypsie to stay out of my personal space is no different
than shouting SHOO at a dog when it comes too close.
That's one racist remark too many. Welcome to my bozo bin.
There isn't a court in Britain or Ireland that would find someone
guilty for punching a Roma gypsie. In fact you'd be hard pushed to
even find a policeman that would pursue the matter.
You appear to be living in the 1970s. Fortunately nowadays in Britain an
unprovoked assault gets prosecuted irrespective of the race of the victim.
Jun 27 '08 #76
Richard Tobin <ri*****@cogsci .ed.ac.ukwrote:
>
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?
Performance. If, for example, word-aligned accesses are more efficient
than non-aligned accesses, an implementation may want to align each
member on at least a word boundary. I know of at least one real
implementation that did, in fact, behave that way. Of course, that
would almost certainly not affect doubles (which tend to have the
strictest alignment requirements anyway), but it might well affect chars
and shorts.

-- Larry Jones

Mom would be a lot more fun if she was a little more gullible. -- Calvin
Jun 27 '08 #77
I think I will use an array and forget about it all together.

typedef struct _vector
{
double coord[3];
}vector;

Jun 27 '08 #78
pereges <Br*****@gmail. comwrites:
I think I will use an array and forget about it all together.

typedef struct _vector
{
double coord[3];
}vector;
Great. But why use the name "_vector"? If you insist on having
disinct names for the struct tag and the typedef, you could use a
trailing underscore or a "_s" suffix. For that matter, you could
just drop the struct tag:

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

(Or you could drop the typedef and just call it "struct vector".)

Names starting with underscores should generally be avoided. I
*think* this one is ok, but the fact that I'm not sure without further
thought is a clue that it's easier just to avoid leading underscores
altogether.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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"
Jun 27 '08 #79
On Jun 17, 3:43 pm, Keith Thompson <ks...@mib.orgw rote:
Great. But why use the name "_vector"? If you insist on having
disinct names for the struct tag and the typedef, you could use a
trailing underscore or a "_s" suffix. For that matter, you could
just drop the struct tag:

typedef struct {
double coord[3];
} vector;
The only reason why I do it is because some times I have come across
situations which require self referential structures.

eg.
typedef struct _tree
{
struct _tree *left;
struct _tree *right;
int data;
}tree;

Not that I will always encounter a situation like this, but I just
tried to keep the naming technique consistent.

(Or you could drop the typedef and just call it "struct vector".)

Names starting with underscores should generally be avoided. I
*think* this one is ok, but the fact that I'm not sure without further
thought is a clue that it's easier just to avoid leading underscores
altogether.
Why drop the typedef ? I thought certain expressions would be more
concise.
Jun 27 '08 #80

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
6879
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
4
2825
by: Thomas Matthews | last post by:
Hi, I'm writing code for an embedded system. In the system a Timer has 4 memory mapped registers of 32-bit lengths in contiguous locations: Timer 0: 0x1000 Configuration register 0x1004 Control register 0x1008 Input register 0x100C Output register.
6
4207
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph 22 of section 6.2.5. If so, that seems to make it difficult to allocate such structures, because sizeof() is not allowed on incomplete types (paragraph 1 of section 6.5.3.4). For instance, I've routinely done things like this: struct foo {...
11
2419
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to follow? Second, have I correctly passed the structure's pointer to the functions in this program?
15
2789
by: damian birchler | last post by:
Hi I'm wondering of what type a structure is. Of course, it is a _structure_, but an array isn't an _array_ either. So of what type is a structure? I'd say a pointer, am I right?
10
2135
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or multiple arrays,etc) I do not understand how to define the structure at run time.i.e.what fields it will contain.
6
3026
by: noone | last post by:
What is the syntax to access members of a structure without explicitly naming the structure in every access? struct mytype { int a; char* b; long c; } IT; How can I access the structure members in a way similar to the old pascal
5
35806
Banfa
by: Banfa | last post by:
I thought I would write a little article about this because it is regularly asked as a question in the Forums. Firstly this is a C++ issue, it is strictly against the rules of C to create a class with no members. This makes sense because the only real use for a structure or class with no data members and virtual functions is as the base from which to derive other classes and structures or as a container for non-virtual methods. The reason...
5
3803
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.