Thread: Trouble With Pointer Arithmetic

  1. #1
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42

    Trouble With Pointer Arithmetic

    I’ve attached two short c programs which I’ll be referencing
    in the text below, if you have a moment please ctex.zip find in this
    LINK>> WeTransfer

    Attachment also here:
    ctex.zip

    It contains the files:


    listing1.6.2Indirection.c and listing1.6.1.c




    Regarding listing1.6.2Indirection.c


    If ptr is the name given to a pointer
    which is pointing to the beginning of the buffer array
    i.e. buffer[0].


    And I’ve set buffer[0] to 435.


    why does:


    printf(“ %f \n”, ptr);


    print 0.00 to the console? Im expecting it to print 435?
    In fact I was initially expecting some long random value
    because ptr is just a variable name?


    whats adds to my confusion is
    also because…


    In listing1.6.1.c


    ptr is pointing to the last element in buffer[1024];


    but when I say:


    buffer[1003] = 500;
    printf(“%f \n”, ptr);


    the console prints 500?


    This is a very confusing result when ptr is
    pointing to the last element of buffer here.
    When I’ve changed a different element althogether?


    What am I not getting ?


    Many Many thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Fauveboy
    I’ve attached two short c programs which I’ll be referencing
    Don't do that. Come up with the smallest and simplest (and compilable) program that demonstrates the problem, then post the code of that here in [code][/code] bbcode tags. If you really cannot do that, it would be better to post the code to a pastebin and link to the pastebin entry.

    Quote Originally Posted by Fauveboy
    If ptr is the name given to a pointer
    which is pointing to the beginning of the buffer array
    i.e. buffer[0].


    And I’ve set buffer[0] to 435.


    why does:


    printf(“ %f \n”, ptr);


    print 0.00 to the console? Im expecting it to print 435?
    In fact I was initially expecting some long random value
    because ptr is just a variable name?
    The correct format specifier to print a pointer is %p, and you would cast the pointer to void*:
    Code:
    printf(" %p \n", (void*)ptr);
    Having done that, you would expect the address of buffer[0] to be printed (i.e., the "some long random value"), not its value (i.e., 435).
    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

  3. #3
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42
    sorry for the previous attachments, ive now posted the code below.
    I understand that there is an appropriate formatter for pointers and
    by using %p you get a number which is the address of what the pointer
    is referencing.


    But I’m curious about the following behaviour thats occuring.


    where the code reads:


    buffer[100] = 765;

    printf(“%f \n”, ptr);


    how does ptr manage to print 765?


    if it was to print a value surely
    it would be a random one as its set to point to the first
    element in buffer?


    With my understanding so far I was initially expecting ptr to print
    a random long value because I’m not using the dereference syntax “*”
    which is used to get any value from it?


    What seems to happen when I use *ptr with prinf and %f (occuring as
    the last instruction at the end of the code) is that a long random
    value prints to the console (opposite to what i expected)




    Whys this behaviour happening?




    Code:
    
     int main()
       {
          double* ptr;
          double buffer[1024];
       
          buffer[0] = 435;
          
          /* ptr prints 765 why? */
          printf("%f \n", ptr);
      
         /*set ptr to the first elements address of buffer */
          ptr = buffer;
          
          buffer[100] = 765;
      
          /* ptr prints 765 why? */
          printf("%f \n", ptr);
          
          /* this prints the address that ptr points to */
          printf("%p \n", ptr);
      
          /* why is void used here? */
          printf("%p \n", (void*)ptr);
      
          /* ppoint ptr to the addres of the last element in buffer */
          ptr = &buffer[1024];
      
          buffer[1003] = 500;
       
          printf("%f \n", ptr);
      
          printf("%f \n", *ptr);
      }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Fauveboy
    where the code reads:


    buffer[100] = 765;

    printf(“%f \n”, ptr);


    how does ptr manage to print 765?
    That is a mix of two problems:
    • At that point ptr had not been given an initial value, so its value is indeterminate, hence any result is to be expected.
    • Trying to print a pointer using %f results in undefined behaviour. In practice, what you're looking at is probably some attempt to interpret the bytes of the pointer value (i.e., the address) as a double, and the result ended up as 765. Or it could be something else, but then you would have to say, examine the assembly output, and maybe even examine the source code of the compiler.


    Quote Originally Posted by Fauveboy
    With my understanding so far I was initially expecting ptr to print
    a random long value because I’m not using the dereference syntax “*”
    which is used to get any value from it?
    Yes, you are right for the second printf call, and that's why you should use %p to print pointers, and %f to print doubles. Mixing up the two merely causes you unnecessary confusion.

    Quote Originally Posted by Fauveboy
    What seems to happen when I use *ptr with prinf and %f (occuring as
    the last instruction at the end of the code) is that a long random
    value prints to the console (opposite to what i expected)
    You set ptr to &buffer[1024]. That means it points one past the end of the buffer. This is a technique used when traversing a range with pointers (i.e., you use another pointer to point to the first element, then iterate by incrementing this second pointer until its value is equal to the one past the end pointer), but one must take care never to deference the pointer. You then dereferenced the pointer, so of course you ended up with a problem, since your pointer doesn't point to a valid object in the buffer.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Arithmetic trouble
    By cjavier in forum C Programming
    Replies: 2
    Last Post: 03-12-2012, 01:28 AM
  2. pointer arithmetic
    By danieldcc in forum C Programming
    Replies: 8
    Last Post: 09-30-2011, 12:17 PM
  3. Pointer Arithmetic
    By taurus in forum C Programming
    Replies: 5
    Last Post: 11-14-2008, 03:28 AM
  4. Pointer arithmetic
    By depietro in forum C Programming
    Replies: 13
    Last Post: 03-29-2007, 06:03 AM
  5. pointer arithmetic
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-04-2001, 06:45 PM

Tags for this Thread