Can't find what '->' is used for.

This is a discussion on Can't find what '->' is used for. within the C Programming forums, part of the General Programming Boards category; I am reading some code ( K&R Questions ) but cannot figure out what '->' means. I have seen it ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    32

    Can't find what '->' is used for.

    I am reading some code (K&R Questions) but cannot figure out what '->' means. I have seen it a few times, and have tried searching around but have not come up with an answer. Just for an example, here is one instance where it is used.

    [snippet]
    Code:
    #include <stdio.h>
    
    
    #define MAXVAL 1000
    #define MAXLINE 1000
    
    
    typedef struct {
        int top;
        int val[MAXVAL];
        int pos[MAXVAL];
    } stackstr;
    
    
    /* very simple stack push function */
    int push(stackstr *stk, int foo, int bar)
    { 
        if (stk->top == MAXVAL) {
            printf("stack overflow. NOT putting more values on the stack.\n");
            return 1;
        }
        stk->val[stk->top] = foo;
        stk->pos[stk->top] = bar;
        stk->top++;
        
        return 0;
    }
    ... [/snippet]

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    When you need to access a value from a pointer to a struct, you have to use the -> notation... struct->value
    When you have access to the struct directly you use the dot notation... struct.value

    In the example you are working with a pointer to a struct... so....

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    When you need to access a value from a pointer to a struct, you have to use the -> notation... struct->value
    When you have access to the struct directly you use the dot notation... struct.value

    In the example you are working with a pointer to a struct... so....
    Ah, makes sense why I have not come across them yet. I just finished my first college semester of C and the book we used, IMHO, was terrible (C programming for the Absolute Beginner).The teacher was actually even worse, she gave me a zero on an assignment once because she thought the source code was corrupted. By corrupted she mean't that when she opened the file in WordPad all the code was on one line.. I almost lost it. So, I went ahead and got 'The C programming language' by K&R, and I am basically relearning everything. There have been quite a few things already in the first chapter of K&R that were never even mentioned in the other book.

    Appreciate the help, seems I will get there soon enough.
    Last edited by Mark Labarbara; 01-01-2012 at 08:51 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mark Labarbara View Post
    so I am basically relearning everything. There have been quite a few things already in the first chapter of K&R that were never even mentioned in the other book.

    Appreciate the help, seems I will get there soon enough.
    Also keep in mind that even the more advanced books like K&R don't cover everything... In the process of absorbing the fundimentals you will learn about perhaps 20 or 30 library functions and basic program structures... However the actual C standard library contains hundreds of functions, and many compilers also come with their own language extensions and extra libraries as well. Do yourself a favour and get the specific library documentation for your compiler... you will be glad you did and you'll need it to check syntax anyway.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Alright I will, should be easy, I use gcc lol.

    Anyways, thanks for the advice.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mark Labarbara View Post
    Alright I will, should be easy, I use gcc lol.

    Anyways, thanks for the advice.
    GCC is very well documented... and far more capable than you'll find out from your books... Visit the website, download every document, man page and help file you can get your hands on... And get ready to be pleasantly surprised.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    GCC is very well documented... and far more capable than you'll find out from your books... Visit the website, download every document, man page and help file you can get your hands on... And get ready to be pleasantly surprised.
    Yeah I have been surfing that site since your last comment. However, I can't seem to find exactly what I think I am looking for. On this page it lists all of the documentation for every version. I found exactly what I want but for C++ (GCC 4.6.2 Standard C++ Library Manual) which goes through all of the standard C++ libraries and headers, etc.. Don't see the same for C.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mark Labarbara View Post
    Yeah I have been surfing that site since your last comment. However, I can't seem to find exactly what I think I am looking for. On this page it lists all of the documentation for every version. I found exactly what I want but for C++ (GCC 4.6.2 Standard C++ Library Manual) which goes through all of the standard C++ libraries and headers, etc.. Don't see the same for C.
    Usually they're included in the C++ stuff for dual mode compilers... Look up some of the standard C headers... string.h, stdio.h etc...

    But you also want the documentation on language extensions, etc. for the compiler itself.

    Looking at the page, I'd want the GCC Manual, The CPP manual (which includes C) and the two C++ library manuals for a start.

    Theres a lot more for C here... http://www.gnu.org/software/libc/manual/
    Last edited by CommonTater; 01-01-2012 at 09:46 PM.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    Usually they're included in the C++ stuff for dual mode compilers... Look up some of the standard C headers... string.h, stdio.h etc...

    But you also want the documentation on language extensions, etc. for the compiler itself.

    Looking at the page, I'd want the GCC Manual, The CPP manual (which includes C) and the two C++ library manuals for a start.

    Theres a lot more for C here... The GNU C Library - GNU Project - Free Software Foundation (FSF)
    Awesome, thanks again!
    I think I have enough to keep myself occupied till next semester starts

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please find a bug !
    By kiss in forum C Programming
    Replies: 3
    Last Post: 12-19-2009, 06:56 AM
  2. std::string::find vs std::find
    By petry in forum C++ Programming
    Replies: 17
    Last Post: 07-08-2009, 06:26 PM
  3. To find corresponding value??
    By N707JT in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 11:19 PM
  4. Where to find
    By golfinguy4 in forum C++ Programming
    Replies: 6
    Last Post: 01-07-2002, 08:18 PM
  5. Can you find the bug? 'Cause we can't
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 11-29-2001, 04:28 PM

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