Thread: Question about pointers

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Question about pointers

    today i was trying some code in pointers whish really amaze me for incrementing but some variable i get weird results
    Code:
    #include <stdio.h>
    int increment_it(int *num)
    {
        int result;
        (*num)++;//this works
        *num=*num+1;//works
    #ifdef USE_NON_WORKING
        *num++;//this doesnt
    #endif
        return result;
    }
    int main(void)
    {
        int x=0;
        increment_it(&x);
        printf("%d",x);
        getchar();
        return 0;
    }
    first i thought *num++; would just increment the address itself but it doesnt even increment anything so any idea what it doess coz this is really weird.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It dereferences the pointer. Then it increments the pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by quzah View Post
    It dereferences the pointer. Then it increments the pointer.
    ...and num is a local variable, so you won't notice any change in main().

    *num++ increments num, not *num. Have a look at the operator precedence rules.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i know i made before that return result=*num; i just forgot to put it. but it
    Code:
    #include <stdio.h>
    int increment_it(int *num)
    {
        int result;
        (*num)++;//this works
        *num=*num+1;//works
    #ifdef USE_NON_WORKING
        *num++;//this doesnt
    #endif
        return result=*num;
    }
    int main(void)
    {
        int x=0;
        increment_it(&x);
        printf("%d",x);
        getchar();
        return 0;
    }
    but isnt *num++ same as *num=*num+1; ?

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by Snafuist View Post
    ...and num is a local variable, so you won't notice any change in main().

    *num++ increments num, not *num. Have a look at the operator precedence rules.

    Greets,
    Philip
    num is a prameter in the function its not a local variable

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by elwad View Post
    num is a prameter in the function its not a local variable
    Yes, but it has the same effect - it is a local copy of the variable used in the caller.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by elwad
    but isnt *num++ same as *num=*num+1; ?
    No, as has been pointed out in this thread, it has net effect equivalent to:
    Code:
    *num;
    num++;
    Quote Originally Posted by elwad
    num is a prameter in the function its not a local variable
    A parameter is a local variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i forgot x=increment_it(&x);
    Code:
    #include <stdio.h>
    int increment_it(int *num)
    {
        int result;
        (*num)++;//this will increment it to 1
        *num=*num+1;//this will increment it to 1
    #ifdef USE_NON_WORKING
        *num++;//this wont increment it to 1
    #endif
        return result=*num;
    }
    int main(void)
    {
        int x=0;
        x=increment_it(&x);
        printf("%d",x);//result 2 coz i increment it twice
        getchar();
        return 0;
    }
    but why does *num++; deference is since i alrdy did in my function x=increment_it(&x); ?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. You do not need the variable result - all you do with it is assign it a value in the same statement as the return statement.

    2. *num++ does a dereference because you have * in the statement. It does this first, then increments num to point to the next integer element after what it originally pointed to. Since you are not actually using the dereferenced value, the compiler just throws it away again (it may not even dereference it at all if you enable enough optimization in the compiler).

    3. The purpose of calling increment_it() with a pointer value seems lost if you also return the new value and assign it to the original value passed in. After all, the function itself has already modified the value pointed to by the pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh i get it now also yah your right i didnt even need to return anything since its alrdy incremented it

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    I am finding little hard to comprehend things

    Hope i am not hijacking the thread.

    But in the following case:

    Code:
    int main()
    {
       int i[] = {3, 5}; 
       int *p = i; 
       int j = --*p++; 
       
       printf("j = %d, p= %d\n\n", j,*p);
       //system("pause");
       return 0;
    }
    Things look little different

    j = 2 and *p = 5

    They say that Postfix (++ and --)has an associativity of left to right ,
    but at the same time we can see a Prefix associated above, having an associativity of right to left.

    Can explain how to comprehend the above line of code. how does the compiler preceive the above statement.

    Thanks to all in advance

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    can someone explain

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we split it and add parenthesis to show what is going on:

    Code:
       int *p = i; 
       int j = --(*p);
       p++;
    So, j is the *p, which is the first element of array i, after it has had 1 subtracted from it (the -- part does that).
    Then p is incremented.

    Writing code like this is VERY bad, and it's unlikely to give any better code generated by the compiler than splitting it to several steps [which then becomes much easier to understand]. Using parenthesis to explain what you are doing, etc, will also help.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM