Thread: *a++ declaration

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    *a++ declaration

    If you have the statement:

    Code:
    *a++ = *b++;
    then indirection will occur first, causing the value at which a points to to be yielded, then the ++ operator will add one to what it points to, right? In my C books, it says that the above statement is equivelent to:

    Code:
    int i = 0;
    a[i] = b[i];
    i++
    Yet, that wouldn't make sense with my previouse statement. Any clarification would be greatly appreciated.

    Thanks in advance.

    Ty
    Last edited by tyskater; 06-17-2004 at 10:11 PM.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I wouldn't really waste all my time memorizing acquard order of precidences. Some will make your code a little more cryptic(nothing wrong with that unless it is too bad) Some are useful, but ambiguous server no purpose really.here is a good one
    Code:
    while(*ptr)
         printf("%c",*ptr++);
    I believe the incrementation is after like in
    Code:
    while(*s++)
    I just am not sure on this example.
    Last edited by linuxdude; 06-17-2004 at 10:08 PM.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    I just don't get how this:

    Code:
    *a++ = *b++;
    Is equivelent (or does basically the same thing) as this:

    Code:
    int i = 0;
    a[i] = b[i];
    i++

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Your book is kind of right. It is kind of like that, it gets the value of the pointer (a[i]), then increments the pointer (i++), but instead of the pointer storing the location, i is, like a counter, instead of letting the pointer count.
    Last edited by chrismiceli; 06-17-2004 at 10:13 PM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    let's use my code as an example
    PHP Code:
    #include <stdio.h>

    int main(void){
            
    int *a,*b;   /*creates two pointers*/
            
    int c[]={0,1,2,3,4,5}; /*an array*/
            
    int d[]={1,2,3,4,5,6}; /*another*/
            
    a=c/*a now pionts to c*/
            
    b=d/*b now points do d*/
            
    *a++=*b++; /*equivalent to *a=*b; a++;b++;*/
                            /*the assignment is first then the ++
                            are done afterwards. the reason they
                            are *sort of* equal is in the assignment*/
                            /*a[i]=b[i] is equal to *a=*b; they are sort
                            of because a and b are incremented after-
                            wards in our example; we don't need a temp variable*/
            
    printf("a == %d\nb == %d\nc == %d\n",*a,*b,c[0]);
            return 
    0;

    PHP Code:
    /*My output*/
    == 1
    == 2
    == 
    Do you understand
    Last edited by linuxdude; 06-17-2004 at 10:19 PM.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    Thanks for your prompt reply's!

    So, let me get this straight; *a++ will first return what a point to, then will increment the POINTER, NOT what it points to? It seems that that declaration (*a++) would end up incrementing what a points to, not incrementing it up the array, because indirection occurs first (*), then ++ would incement upon that.
    Thanks..
    Ty

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    Linuxdude:

    Thanks! That totally cleared it up for me. Makes way more sense now.

    Ty

  8. #8
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    No problem. Anytime you don't understand something try to make a program out of it and see what happens. If not then just ask us

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    161
    Quote Originally Posted by tyskater
    Thanks for your prompt reply's!

    So, let me get this straight; *a++ will first return what a point to, then will increment the POINTER, NOT what it points to? It seems that that declaration (*a++) would end up incrementing what a points to, not incrementing it up the array, because indirection occurs first (*), then ++ would incement upon that.
    Thanks..
    Ty
    Actually, the increment is done first, then the indirection, then the assignment. You have to remember that the result of a post-increment expression is value of the operand before the increment:
    Code:
    int a = 3;
    printf("%d", a++);
    //prints 3
    So, even though the pointer is incremented, the indirection actually takes place on the old value of the pointer.

    Hope that helps.


    -tf

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually, the increment is done first, then the indirection, then the assignment. You have to remember that the result of a post-increment expression is value of the operand before the increment:
    POST increment is always done AFTER the rest.
    So
    Code:
    *a++ = *b++;
    is the same as:
    Code:
    *a = *b;
    a++;
    b++;
    Although the order of in which the pointers are increments are undefined I believe.

    tyskater let me give me you a piece of advice: When in doubt use ().

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    Thanks Thantos.
    Do you know why, though, that what 'a' POINTS to isn't incrmented?
    Here is an example:

    Code:
    *&a
    In this code, the & operator will return the address of a, then, the indirection operator will work on what the '&' yielded. Causing it to go TO the address of a. Yet, in *a++, you guys are saying that the indirection operator works first, going to where a points to, then, somehow the asignment takes place before the incrmentation. as in:

    Code:
    *a++ = *b++
    Why would the '++' operator do it's "stuff" after the asignment? I thought that postfix meant it would occur after the other operators were through, not after the asignment.
    Thanks again for all your help.

    ty

  12. #12
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    It is after so that it can be ready for another iteration of a loop for example. That makes it easy to use and then increment all in one step.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Its all about the order of precedence. And looking back at what thefroggy said he is correct in a way. the ++ has a higher precedence than * but post increments are always done after the current "line"

    The order of precedence tells you how to bind things. Another way to write
    Code:
    *a++ = *b++;
    is
    Code:
    *(a++) = *(b++);

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I just don't get how this:
    >>*a++ = *b++;
    If you don't understand it, just don't use it. There's no need to write code that is confusing to the reader when you have other means to.

    >>In my C books, it says that the above statement is equivelent
    Not really. If the first set of code, the pointer are incremented and therefore no longer point to their original location. The second code doesn't increment therefore the pointer still points to the original address. In can understand what the book is saying, but its a poor example, imho.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM