Re: why use -> (not .) with pointers?



In article <1120137370.348589.90000@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Suman <skarpio@xxxxxxxxx> wrote:

>-> is used when you have a pointer to a struct, and you are trying to
>access elements of the struct whose address is stored in the pointer.
>
>. is used when you have an object of a struct, and you are trying to
>access elements of the struct.

I think the original poster knows that. Since there is no context
where both a.b and a->b are legal, he is asking why C doesn't use the
same syntax for both.

I think the answer is just that they are different operations so C has
different names for them. a->b is equivalent to (*a).b. A less
explicit language than C might confound these, but C doesn't.

-- Richard

.



Relevant Pages

  • Re: Another spinoza challenge
    ... You should test against the int type's limits: ... typedef struct complex ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: how to store list of varying types
    ... represent that with those in the struct definitions? ... pointer null, and the second one the CString object. ... then have to finish constructing the packet by copying the two data objects ... start with an upper-case letter (so, I would use DataElement ...
    (microsoft.public.vc.mfc)
  • Re: N1298 - try/finally for C
    ... Do you mean that "struct throwInfo" should be optional? ... that exception handling would be supported only on implementations ... If the standard mandates an integer as throw argument, ... should be an integer wide enough to encode a pointer if necessary. ...
    (comp.std.c)
  • Re: [RFC][PATCH 1/4] cgroup: support per cgroup subsys state ID (CSS ID)
    ... Patch for Per-CSS(Cgroup Subsys State) ID and private hierarchy code. ... This patch attaches unique ID to each css and provides following. ... returns pointer to struct cgroup_subysys_state of id. ... using a pointer introduces no such restriction. ...
    (Linux-Kernel)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)

Quantcast