What does this symbol mean: ->

This is a discussion on What does this symbol mean: -> within the C Programming forums, part of the General Programming Boards category; as in: something->new thanks and happy new year! thanks...

  1. #1
    deregistered
    Guest

    Question What does this symbol mean: ->

    as in:

    something->new

    thanks and happy new year!

    thanks

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    For C....

    If you have a structure called mystruct with a member called var, you would access var with mystruct.var.

    If you had a pointer to the structure, you would use mystruct->var

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    197

    a good advice:

    Buy a good book about C, for example "The C Programming Language" and learn some C, or read some reference.

    klausi
    When I close my eyes nobody can see me...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    struct FOO
    {
        char *stuff = "floobie";
    };
    
    int main(void)
    {
        struct FOO foo;
        struct FOO *pFoo;
    
        puts(foo.stuff); /*This will work, prints floobie*/
        puts(pFoo->stuff); /*okay here too, prints floobie*/
    
        puts(foo->stuff); 
        /*bzzzzt, wrong answer!
        **foo is a direct instance, not a pointer
        **-> is used to with a pointer to a struct
        */
        puts(pFoo.stuff);
        /*bzzzzt, also wrong!
        **pFoo is a pointer, treat it like one
        ** the . operator is used for a direct instance only
        */
    
        return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'll make it even easier to understand:

    some_struct->element is exactly the same as saying (*some_struct).element

    But who wants to dereference a struct just to access its elements? It is a shortcut, like i++ or x |= 7.
    Last edited by master5001; 12-31-2001 at 01:52 AM.

  6. #6
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    a helpful aspect of this deferencing is that passing a pointer to a user defined conglomerate type as an argument to a function is a lot less expensive on the stack than passing the whole instance of the type...
    hasafraggin shizigishin oppashigger...

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I had been wondering the same thing. Thanks for the answer.
    master5001 I have never seen x | = 7 before. What does the |= do?

  8. #8
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    x |= 7

    It's like saying x = x | 7

    the | symbol is the bitwsie OR.

    It basically works like this...let's say you have two numbers, 2 and 1

    0000000010 (binary 2)
    0000000001 (binary 1)

    well when you bitwise OR them, it comes out like this:
    Code:
     0000000010
              | (OR)
     0000000001
    ---------------------
     0000000011
    which is 3

    see what it did? Every there was a 1 in a place that there wasn't one in the other number, it put a 1 there. You can basically think of it as adding or combining numbers in binary (but only think of it like that in binary terms, because 6 | 6 != 12, that 2 | 1 = 3 is just sorta a conincidence.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    thanks Ken
    That is easy enough to understand.

  10. #10
    Sayeh
    Guest
    It means "go through".

    So if you have a pointer to and structure named 'bunnywabbit'. You can "go through" it (called indirection) to access it's fields.

    [code]
    typedef struct
    {
    int ears;
    int feet;
    int whiskers;
    int tail;
    }wabbit;


    wabbit *bunnywabbit;

    bunnywabbit = (wabbit*)malloc(sizeof(wabbit));

    bunnywabbit->ears = 2;
    bunnywabbit->feet = 4;
    bunnywabbit->whiskers = 6;
    bunnywabbit->tail = 1;

    [\code]

    enjoy.

  11. #11
    Sayeh
    Guest

    sorry, code tag didn't work

    Try code tag the other way (forgive my sorry old head)--

    Code:
     
    typedef struct 
       { 
       int ears; 
       int feet; 
       int whiskers; 
       int tail; 
       }wabbit; 
    
    
    wabbit *bunnywabbit; 
    
    bunnywabbit = (wabbit*)malloc(sizeof(wabbit)); 
    
    bunnywabbit->ears = 2; 
    bunnywabbit->feet = 4; 
    bunnywabbit->whiskers = 6; 
    bunnywabbit->tail = 1;
    enjoy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 03:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21