Re: [OFF-TOPIC/C] Acessing members (-> and .)

From: Daniel Koepke (dkoepke@CALIFORNIA.COM)
Date: 11/22/97


{BTW, I'm in the acursed Win95 using Netscape Communicator at the moment, so
 forgive me if the posting comes out odd looking, or has anomolies, I've never
done this
 before :\)

Chuck Reed wrote:

> I have seen a lot of new stuff in C, but one thing is just really escaping
> me.  I cannot seem to figure out all the *->ch or *->* or whatever.  What in
> gods name does this "->" mean?  I have looked in my on-line C tutorials and
> I can't seem to find it.

Okay, really quickly: there's two ways to access a member of a structure, with a
'.' or
with a '->'.  They do the same thing, except for different types of structures.
The '.'
notation is used for non-pointers, and the '->' used for pointers.  Example:

  struct char_data *ch; /* is a pointer */
  for (ch = char_linked_list; ch; ch = ch->next) /* note the "ch->next" */

for pointers we can't do "ch.next".  For non-pointers we use '.':

  struct stupid_struct ss;
  ss.data = strdup("This isn't a pointer.");

There is, of course, one other exception.  You use '.' for dynamically allocated
arrays that
use the bracket-notation for indexing.  So, to give an example:

  extern struct room_data *world; /* note this is a pointer */
  for (ch = world[ch->in_room].people; ch; ch = ch->next_in_list)

This is because when we use the bracket-notation we are accessing a single
element in the
dynamic array, not the pointer/dynamic array itself.  However, when we use
pointer math
to refer to an element, we are still dealing with a pointer.  For instance:

  extern struct room_data *world;
  int i;
  i = (world+ch->in_room)->number;

Anyway, hopefully this message comes out okay and Netscape doesn't decide to use
a bunch
of HTML and garbage.


daniel koepke / dkoepke@california.com


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST