delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/26/00:18:01

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: structures and structure pointers
Date: Thu, 23 Oct 1997 09:11:34 -0700
Organization: Alcyone Systems
Lines: 63
Message-ID: <344F7736.13283EEC@alcyone.com>
References: <19971023043001 DOT AAA27550 AT ladder02 DOT news DOT aol DOT com>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Joshua Hab wrote:

> First, what's the difference between a regular structure and a typedef
>  structure? For example, what would be the difference between these two:
> 
> struct sprite
> {
>     int x, y, color;
> }
> 
> typedef struct SPRITE
> {
>     int x, y, color;
> }

Well, these aren't complete, for one.

In C or C++, you can write:

    typedef struct S { /* ... */ } S;

which allows you to refer to this struct as either `struct S' or just
`S'.

C++ does away with the need for the struct keyword when referencing the
type, so you can just write 

    struct S { /* .... */ };

and you'll be able to reference it as either `struct S' or `S'.  In C,
the above definition could only be referred to as `struct S'; `S' would
give an error.

> Secondly, how exactly does the -> operator work? How is it different
> from the
>  '.' operator? I've know it has something to do with structure
> pointers, but
>  I'm still baffled, I'd really appreciate some direction. :-)  Thanks
> in advance.

The -> operator is used when the left operand is a pointer to a
structure, rather than a structure itself.  That is:

    struct S { int x, y, z; };

    S s = { 1, 2, 3 };

    int x = s.x;

    S *sp = &s;

    int y = s->y;

Writing a->b is exactly the same as writing (*a)->b -- it is a
convenient notation.

-- 
         Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
                       Alcyone Systems / http://www.alcyone.com/max/
  San Jose, California, United States / icbm://+37.20.07/-121.53.38
                                     \
  "After each war there is a little / less democracy to save."
                                   / Brooks Atkinson

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019