Thread: question about incrmenting pointer vs array?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    29

    question about incrmenting pointer vs array?

    why does this print "12345"

    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int num[] = {1,2,3,4,5};
    int *pnum = num;
    
    for(pnum;pnum <= num+4;pnum++)
        printf("%d",*pnum);
    
    return 0;
    }
    but this doesn't work (just prints garbage values)

    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int num[] = {1,2,3,4,5};
    int *pnum = num;
    
    for(pnum;pnum <= pnum+4;pnum++)
        printf("%d",*pnum);
    
    return 0;
    }
    if num and pnum have the same starting address and you can increment pnum as pnum++, why doesn't pnum+4 = num+4


    thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First both you snippets have an issue with the for statement. The initialization section is incorrect. Since you have already initialized pnum you can leave that section blank.
    Code:
    for(;pnum <= pnum+4;pnum++)
    Next in the condition clause you are always comparing to the same variable, pnum will always be less than pnum+4. Maybe you should be using a different variable? Maybe something like:
    Code:
    for(;pnum < num + 5;pnum++)
    Both of your snippets have this problem.

    Jim

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    29
    well, i guess that is my question. why is pnum+4 != num+4 if both are the same starting address? what does pnum+4 actually point to, if it is not pointing to the 5th element in num?

    thanks

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    what does pnum+4 actually point to, if it is not pointing to the 5th element in num?
    O_o

    Without considering numeric overflow, always the fourth element beyond `pnum'.

    Read what Jim has to say again, and then consider what would change, mechanically, if `pnum' was just an integer.

    Soma

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you never print pnum+4
    use following code and see what it actually point to

    Code:
    #include<stdio.h>
     
    int main()
    {
     
    int num[] = {1,2,3,4,5,6,7,8,9,10};
    int *pnum = num;
     
    for(pnum;pnum <= num+4;pnum++)
        printf("%d %d\n",*pnum, *(pnum+4));
     
    return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The problem is not to where pnum is pointing or even where pnum + 4 is pointing. The problem is that you change pnum in your increment section, which means you are changing both sides of the equation, you should only be changing one side of the equation. You need one side of the increment equation to be constant.

    what does pnum+4 actually point to, if it is not pointing to the 5th element in num?
    Until you change pnum in the increment section of the loop pnum + 4 points to the 4th element, not the 5th. But when you increment pnum pnum + 4 now points to the 5th element, the next time through the loop it would point to the 6th element, which is outside the bounds of your array.


    Jim

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    29
    wow, i feel stupid. that would be like doing ";i< i+5;i++". i was so focused on the addresses i didn't realize if was affecting both sides.
    this leads me to another question though. how come "num+4" can be evaluated in the inequality as an element of array num, but num++ is illegal because it's assigning the array a new value?

    thanks.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Because you statically allocated the array, so you can't change where the pointer points, it's constant.

    Jim

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    29
    Quote Originally Posted by jimblumberg View Post
    Because you statically allocated the array, so you can't change where the pointer points, it's constant.

    Jim
    how does the compiler know num+4 isnt an attempt to change the array as num++ would, if num++ == num+1, or is it not the same? Or does the compiler always recognize the "a+N" format as &a[N]

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    num++;
    could be translated into
    Code:
    num= num+1;
    there is no problem with num+1 part. There is problem with assigning result value back to the variable which could not be changed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by fazio93 View Post
    how does the compiler know num+4 isnt an attempt to change the array as num++ would, if num++ == num+1, or is it not the same? Or does the compiler always recognize the "a+N" format as &a[N]
    In this case, the compiler knows that num is the name of an array with a fixed address or a fixed offset on the stack, and as mentioned above, the compiler won't let you change the value of that fixed address or fixed offset. The compiler will let you use the name of the array as if it were a pointer in the case of num[N] or *(num+N), but it won't let you change the address or offset that num represents.
    Last edited by rcgldr; 06-24-2013 at 02:18 AM.

  12. #12
    Registered User
    Join Date
    May 2013
    Posts
    29
    Oh, alright. Because pnum < num+4 isn't attempting to reassign num. i understand.

    Thank you all for clearing everything up. Much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointer question
    By raczzoli in forum C Programming
    Replies: 9
    Last Post: 07-22-2011, 09:12 AM
  2. pointer to an array question..
    By transgalactic2 in forum C Programming
    Replies: 41
    Last Post: 10-20-2008, 12:22 PM
  3. pointer/ array question..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 10-14-2008, 05:06 PM
  4. pointer / array question
    By FH|RogueHunter in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2005, 07:17 PM
  5. Array/Pointer question
    By Swaine777 in forum C++ Programming
    Replies: 2
    Last Post: 05-17-2003, 10:29 PM