can anyone explain hte "->" operator in c, i read it in a for loop
Results 1 to 5 of 5

Thread: can anyone explain hte "->" operator in c, i read it in a for loop

  1. #1
    Junior Member
    Join Date
    Apr 2005
    Posts
    17

    can anyone explain hte "->" operator in c, i read it in a for loop

    Well i read it in a source code of a program and have been banging my head to learn more.
    Also can any one explain how to initialize random number generator, why should i ise srand() only.
    I also have been reading c for (on and off) for a year or so, but i still dont understand codes of open source programs(i am in first year of under graduation does any thing have to do with that).
    are there any websites that you suggest(apart from topcoder) that partial newbies can visit without becomming intimidated(by the full weight of complexity of prgramming,i still realise that thats the bitter truth).
    you look up and a bird shits in the sky ,you fall on your knees and thank god that buffalos dont fly.

  2. #2
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Ok, the -> operator I can explain (I'm not sure about random number generation since I've never bothered to look it up). Say you have a structure:

    Code:
    typedef struct {
    	int a;
    	int b;
    } MyStruct;
    You could declare the structure like this:

    Code:
    MyStruct m;
    If you now want to access the variable "a" in m, you would type "m.a" (i.e. access "a" from structure "m"). Now before I go any further, a quick intro to pointers.

    Say you have a pointer to an integer, if you tried to treat it as a normal integer and assigned it the value 10, this would be incorrect because what you are actually saying is set the memory address of my integer to 10 (since a pointer points to a memory address). If you want to access the value pointed to by your pointer, you have to dereference it. Here's an example to make it a bit clearer:

    Code:
    // create an integer and then create a pointer that points to it:
    int a = 0;
    int *p = &a; // assign p the memory address of a
    
    p = 10; // this would not give p the value 10, but would assign
    	// it the memory address 10 which you probably
    	// wouldn't want to do
    
    // dereference p to access the value stored in a:
    *p = 10;
    Now say you have the structure MyStruct from above and you create a pointer to it - in order to access the values stored in the structure you would have to dereference it. There are two ways to do it:

    Code:
    MyStruct m;
    MyStruct *p = &m;
    
    // The first (and annoying - typing-wise - way to do it
    *p.a = 10;
    *p.b = 20;
    
    // The second, more common way to do it
    p->a = 10;
    p->b = 20;
    So there you are, the "->" operator is a "quick" way of dereferencing a structure pointer and accessing a member of the structure. If you are finding this difficult to understand, just think to yourself whenever you see "x -> y" that what it says is "access y from (that is stored in) x".

    Apologies if there are any inaccuracies in what I've said or terms used incorrectly - I've been using c# for the last while and haven't touched c for ages.

    ac

  3. #3
    Senior Member
    Join Date
    Oct 2001
    Posts
    872
    I happen to like the *p.a, *p.b notation.

    Sue me.


    And if you want to learn C, forget about trying to browse sites. Get your hands on some books.
    ...This Space For Rent.

    -[WebCarnage]

  4. #4
    Too bad that operator precedence will have that interpreted in a way that is probably unlike what you wanted. That would access what a or b pointed to, if they were pointers, and otherwise give you illegal indirection. You would need

    Code:
    (*p).a;
    (*p).b;
    And so there's this whole arrow shorthand dealio.

    Code:
    p->a;
    p->b
    It really is just convenient shorthand.

  5. #5
    Junior Member
    Join Date
    Apr 2005
    Posts
    17
    Thanks for the detailed explanation gothic_type, and others for the comments.
    you look up and a bird shits in the sky ,you fall on your knees and thank god that buffalos dont fly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides