"." vs "->" operators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben

    #1

    "." vs "->" operators

    (I am using gcc 3.2 on RH 8)

    int name (struct str *name)

    I call the above function like this:
    struct str buf;
    int conf = name(&buf);
    int j;

    for (j=0; buf.address[j]; j++) {
    printf("%c", buf.address[j]);
    }

    If I do buf->address[j] instead of buf.address[j], it gives me error..
    Why's that? aren't they the same thing?

    Bit confused!
    Ben
  • Mark Henning

    #2
    Re: ".&quot ; vs "->" operators

    Ben wrote:[color=blue]
    > If I do buf->address[j] instead of buf.address[j], it gives me error..
    > Why's that? aren't they the same thing?[/color]

    No. buf->address[j] is equivelant to (*buf).address[j]


    Comment

    • Mike Wahler

      #3
      Re: ".&quot ; vs "->" operators

      "Ben" <crescent_au@ya hoo.com> wrote in message
      news:d99e1341.0 407130632.23604 617@posting.goo gle.com...[color=blue]
      > (I am using gcc 3.2 on RH 8)
      >
      > int name (struct str *name)
      >
      > I call the above function like this:
      > struct str buf;
      > int conf = name(&buf);
      > int j;
      >
      > for (j=0; buf.address[j]; j++) {
      > printf("%c", buf.address[j]);
      > }
      >
      > If I do buf->address[j] instead of buf.address[j], it gives me error..[/color]

      It should.
      [color=blue]
      > Why's that? aren't they the same thing?[/color]

      Not at all. Why would you think so?

      '.' is used to select a member from a struct
      object 'directly' , using the struct object's name.

      '->' is used to select a member from a struct object
      'indirectly', using a pointer to that struct object.

      struct s
      {
      int member;
      };

      struct s obj; /* struct */
      struct s *p = &obj; /* pointer to struct */

      obj.member; /* (1) direct access */
      p->member; /* (2) indirect access, via a pointer */
      (*p).member; /* (3) same as (2) */


      (2) is simply 'shorthand' notation for (3).

      Inside your function 'name()', you can access the struct
      members with '->' (since the parameter is a pointer to
      a struct). e.g. name->address. Alternatively,
      (*name).address

      BTW your function and its parameter have the same identifier.
      Don't Do That. :-)


      -Mike


      Comment

      • Eric Sosman

        #4
        Re: &quot;.&quot ; vs &quot;-&gt;&quot; operators

        Ben wrote:[color=blue]
        > (I am using gcc 3.2 on RH 8)
        >
        > int name (struct str *name)
        >
        > I call the above function like this:
        > struct str buf;
        > int conf = name(&buf);
        > int j;
        >
        > for (j=0; buf.address[j]; j++) {
        > printf("%c", buf.address[j]);
        > }
        >
        > If I do buf->address[j] instead of buf.address[j], it gives me error..
        > Why's that? aren't they the same thing?[/color]

        Both notations work with structs (or unions) and
        designate the element named on the right-hand side.
        The difference is in what's on the left-hand side: for
        `.' the l.h.s. is an actual struct, while for `->' it's
        a pointer to a struct.

        struct str buf; /* a struct object */
        struct str *ptr = &buf; /* a pointer to it */

        buf.thing = 42; /* assign to a struct element */
        ptr->thing = 42; /* assign via a pointer */

        buf->thing = 42; /* error: l.h.s. not a pointer */
        ptr.thing = 42; /* error: l.h.s. not a struct */

        Actually, the `->' operator could be done away with, at
        some cost in verbosity:

        (*ptr).thing = 42;

        Since `ptr' points to a struct, `*ptr' is the struct
        itself, and we can apply the `.' operator to it. (We
        need the parentheses because `*ptr.thing' would be the
        interpreted as `*(ptr.thing)', which is not what we want.)

        --
        Eric.Sosman@sun .com

        Comment

        • Mark A. Odell

          #5
          Re: &quot;.&quot ; vs &quot;-&gt;&quot; operators

          crescent_au@yah oo.com (Ben) wrote in
          news:d99e1341.0 407130632.23604 617@posting.goo gle.com:
          [color=blue]
          > (I am using gcc 3.2 on RH 8)
          >
          > int name (struct str *name)
          >
          > I call the above function like this:
          > struct str buf;
          > int conf = name(&buf);
          > int j;
          >
          > for (j=0; buf.address[j]; j++) {
          > printf("%c", buf.address[j]);
          > }
          >
          > If I do buf->address[j] instead of buf.address[j], it gives me error..
          > Why's that? aren't they the same thing?[/color]

          Sure it does. -> is for pointers to struct/unions whereas . is for
          struct/unions. E.g.

          struct str Str;
          struct str *pStr = &str;

          if (Str.address[0] != pStr->address[0])
          {
          printf("The world has broken\n");
          }

          --
          - Mark ->
          --

          Comment

          • Martin Ambuhl

            #6
            Re: &quot;.&quot ; vs &quot;-&gt;&quot; operators

            Ben wrote:[color=blue]
            > (I am using gcc 3.2 on RH 8)
            >
            > int name (struct str *name)
            >
            > I call the above function like this:
            > struct str buf;
            > int conf = name(&buf);
            > int j;
            >
            > for (j=0; buf.address[j]; j++) {
            > printf("%c", buf.address[j]);
            > }
            >
            > If I do buf->address[j] instead of buf.address[j], it gives me error..[/color]

            Of course it does. 'buf' is a struct, not a pointer to a struct.
            [color=blue]
            > Why's that? aren't they the same thing?[/color]

            No. A pointer to a thing is not the thing.
            Avoiding the evil name 'str':
            struct s b1, *b2;
            b2 = &b1;

            These refer to the same thing:
            b1.address[j]
            b2->address[j]
            (*b2).address[j]
            (&b1)->address[j]
            (*(&b1)).addres s[j]

            Comment

            • Tim Hagan

              #7
              Re: &quot;.&quot ; vs &quot;-&gt;&quot; operators

              Tim Hagan
              "Martin Ambuhl" <mambuhl@earthl ink.net> wrote in message
              news:2lip5rFcuj s7U1@uni-berlin.de...

              [snip]
              [color=blue]
              > Avoiding the evil name 'str':[/color]

              Huh? There is nothing wrong with 'str'. It is a valid identifier.

              Regarding function names, WG14/N869 states:

              7.26.10 General utilities <stdlib.h>

              [#1] Function names that begin with str and a lowercase
              letter (possibly followed by any combination of digits,
              letters, and underscore) may be added to the declarations in
              the <stdlib.h> header.

              7.26.11 String handling <string.h>

              [#1] Function names that begin with str, mem, or wcs and a
              lowercase letter (possibly followed by any combination of
              digits, letters, and underscore) may be added to the
              declarations in the <string.h> header.

              So 'str' is even a valid function name.

              --
              Tim Hagan


              Comment

              • Dan Pop

                #8
                Re: &quot;.&quot ; vs &quot;-&gt;&quot; operators

                In <d99e1341.04071 30632.23604617@ posting.google. com> crescent_au@yah oo.com (Ben) writes:
                [color=blue]
                >(I am using gcc 3.2 on RH 8)
                >
                > int name (struct str *name)
                >
                >I call the above function like this:
                > struct str buf;
                > int conf = name(&buf);
                > int j;
                >
                > for (j=0; buf.address[j]; j++) {
                > printf("%c", buf.address[j]);
                > }
                >
                >If I do buf->address[j] instead of buf.address[j], it gives me error..
                >Why's that? aren't they the same thing?[/color]

                The simplest answer is:

                .. is the simple selection operator, expecting an operand with structure
                type at its left. -> is the indirection and selection operator, expecting
                an operand with pointer to structure type at its left.

                ptr -> field is actually a (handy) shortcut for (*ptr).field.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Dan Pop

                  #9
                  Re: &quot;.&quot ; vs &quot;-&gt;&quot; operators

                  In <2lip5rFcujs7U1 @uni-berlin.de> Martin Ambuhl <mambuhl@earthl ink.net> writes:
                  [color=blue]
                  >Avoiding the evil name 'str':[/color]

                  str is never, in any context, an evil name. It is an evil *prefix* for
                  user identifiers, but not an evil user identifier.

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de

                  Comment

                  Working...