Thread: where is my mistake following this code..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    where is my mistake following this code..

    next to every line i commented on what i think it does

    where is my mistake in understanding of changing the index of the pointer
    ??
    Code:
    #include <stdio.h>
    
    int main() {
      int *ptr;
      int arrayInts[10] = {1,2,3,4,5,6,7,8,9,10};
    
      ptr = arrayInts;                                                                             //ptr points at cell [0]=1 
      printf("The pointer is pointing to the first ");
      printf("array element, which is &#37;d.\n", *ptr);                              //print 1
      printf("Let's increment it.....\n");  
    
      ptr++;                                                                                          //ptr points at cell [1]=2
    
      printf("Now it should point to the next element,");
      printf(" which is %d.\n", *ptr);                                                      //print 2
      printf("But suppose we point to the 3rd and 4th: %d %d.\n",     // print cells [2] [3] 3 4
              *(ptr+1),*(ptr+2));                                                               //ptr stays the same
    
      ptr+=2;                                                                                        //ptr points at cell [3]=4
    
      printf("Now skip the next 4 to point to the 8th: %d.\n", 
              *(ptr+=4));                                                                       //ptr points to cell[7]=8
                                                                                                       //and we print the old ptr value= 4 
      
    ptr--;                                                                                          //ptr points to cell[6]=7
    
      printf("Did I miss out my lucky number %d?!\n", *(ptr++));     //ptr points to cell[7]=8
                                                                                                        //and prints the old ptr value 7
    
      printf("Back to the 8th it is then..... %d.\n", *ptr);                //ptr points to cell[7]=8
                                                                                                         //and prints it
    
      return 0;
    }

    my output :
    1
    2
    3 4
    4
    7
    8

    i am supposed to get the output:

    The pointer is pointing to the first array element, which is 1.
    Let's increment it.....
    Now it should point to the next element, which is 2.
    But suppose we point to the 3rd and 4th: 3 4.
    Now skip the next 4 to point to the 8th: 8.
    Did I miss out my lucky number 7?!
    Back to the 8th it is then..... 8.


    the differnce comes from this line
    ptr+=4
    you said that first we print the old value then we add 4

    looks like they first added 4 then printed the value

    ??
    Last edited by transgalactic2; 10-18-2008 at 11:37 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Since this program seems to work perfectly, I don't understand what you don't understand.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Your comments make a strange kind of inverted "fencepost" error:

    http://www.answers.com/topic/fencepost-error

    Why would you think that cell[7] should contain 8?
    [edit] actually that's my mistake, not yours
    Last edited by MK27; 10-18-2008 at 12:51 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by transgalactic2 View Post
    Code:
    ptr+=2;                                                    //ptr points at cell [3]=4
    
    printf("Now skip the next 4 to point to the 8th: %d.\n", 
              *(ptr+=4));                                      //ptr points to cell[8]=9
                                                               //and we print the new ptr
      
    ptr--;                                                     //ptr points to cell[7]=8
    
    printf("Did I miss out my lucky number %d?!\n", *(ptr++)); //ptr points to cell[8]=9
                                                               //and prints it
    
    printf("Back to the 8th it is then..... %d.\n", *ptr);     //ptr points to cell[8]=9
                                                               //and prints it
    Well I dont know about you, but 3+4 = 7 for me.

    Also, ptr++ is a post increment. The increment doesn't actually happen until after the expression is evaluated. So that would print the element first, and then point to the next cell.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    next to every line i commented on what i think it does

    where is my mistake in understanding of changing the index of the pointer
    ??
    Code:
    #include <stdio.h>
    
    int main() {
      int *ptr;
      int arrayInts[10] = {1,2,3,4,5,6,7,8,9,10};
    
      ptr = arrayInts;                                                                             //ptr points at cell [0]=1 
      printf("The pointer is pointing to the first ");
      printf("array element, which is &#37;d.\n", *ptr);                              //print 1
      printf("Let's increment it.....\n");  
    
      ptr++;                                                                                          //ptr points at cell [1]=2
    
      printf("Now it should point to the next element,");
      printf(" which is %d.\n", *ptr);                                                      //print 2
      printf("But suppose we point to the 3rd and 4th: %d %d.\n",     // print cells [2] [3] 3 4
              *(ptr+1),*(ptr+2));                                                               //ptr stays the same
    
      ptr+=2;                                                                                        //ptr points at cell [3]=4
    
      printf("Now skip the next 4 to point to the 8th: %d.\n", 
              *(ptr+=4));                                                                       //ptr points to cell[7]=8
                                                                                                       //and we print the old ptr value= 4 
      
    ptr--;                                                                                          //ptr points to cell[6]=7
    
      printf("Did I miss out my lucky number %d?!\n", *(ptr++));     //ptr points to cell[7]=8
                                                                                                        //and prints the old ptr value 7
    
      printf("Back to the 8th it is then..... %d.\n", *ptr);                //ptr points to cell[7]=8
                                                                                                         //and prints it
    
      return 0;
    }

    my output :
    1
    2
    3 4
    4
    7
    8

    i am supposed to get the output:

    The pointer is pointing to the first array element, which is 1.
    Let's increment it.....
    Now it should point to the next element, which is 2.
    But suppose we point to the 3rd and 4th: 3 4.
    Now skip the next 4 to point to the 8th: 8.
    Did I miss out my lucky number 7?!
    Back to the 8th it is then..... 8.


    the differnce comes from this line
    ptr+=4
    you said that first we print the old value then we add 4

    looks like they first added 4 then printed the value

    ??

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by transgalactic2 View Post
    the differnce comes from this line
    ptr+=4
    you said that first we print the old value then we add 4

    looks like they first added 4 then printed the value

    ??
    WHAT? Did you even read my post? I didn't say anything about the += operator....

    I spotted 2 simple mistakes you made, highlighted them in red, and gave simple explanations for them.

    And would it kill you to preview your post before hitting submit, and LINE UP YOUR COMMENTS. The formatting you used is very difficult to read, and I think its pretty rude to post a giant mess of text and then ask people to help you with it. At least put SOME effort into your post.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ptr+=4 p++ arent acting on the same principle??

    its a shortcut method for incrementing ptr

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    transgalactic2! I compiled and ran your program and my output was exactly what you say you are supposed to get! Check again!

    1
    2
    3 4
    8
    7
    8
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    MK27, in his first post, he asked where he made a mistake in his comments.

    Now we have a slight problem here, because he seems to be editing his posts, so we have no reference to go back to. SO, I am going to post the original code he had up, PROPERLY FORMATTED so its easier to read.

    Code:
    #include <stdio.h>
    
    int main() {
      int *ptr;
      int arrayInts[10] = {1,2,3,4,5,6,7,8,9,10};
    
      ptr = arrayInts;                                             //ptr points at cell [0]=1 
      printf("The pointer is pointing to the first ");
      printf("array element, which is %d.\n", *ptr);               //print 1
      printf("Let's increment it.....\n");
    
      ptr++;                                                       //ptr points at cell [1]=2
    
      printf("Now it should point to the next element,");
      printf(" which is %d.\n", *ptr);                             //print 2
      printf("But suppose we point to the 3rd and 4th: %d %d.\n",  //print cells [2] [3] 3 4
              *(ptr+1),*(ptr+2));                                  //ptr stays the same
    
      ptr+=2;                                                      //ptr points at cell [3]=4
    
      printf("Now skip the next 4 to point to the 8th: %d.\n", 
              *(ptr+=4));                                          //ptr points to cell[8]=9
                                                                   //and we print the new ptr
      
      ptr--;                                                       //ptr points to cell[7]=8
    
      printf("Did I miss out my lucky number %d?!\n", *(ptr++));   //ptr points to cell[8]=9
                                                                   //and prints it
    
      printf("Back to the 8th it is then..... %d.\n", *ptr);       //ptr points to cell[8]=9
                                                                   //and prints it
    
      return 0;
    }
    There, now we can see where his original errors were, and from what I can tell, there were 2.

    1)
    Code:
      ptr+=2;                                                      //ptr points at cell [3]=4
    
      printf("Now skip the next 4 to point to the 8th: %d.\n", 
              *(ptr+=4));                                          //ptr points to cell[8]=9
                                                                   //and we print the new ptr
    He made simple math error here. After ptr += 2, he correctly points out that ptr points to cell 3. Then on the next line, after (ptr += 4), he says that ptr points to cell 8.

    3 + 4 != 8

    2)
    Code:
      printf("Did I miss out my lucky number %d?!\n", *(ptr++));   //ptr points to cell[8]=9
                                                                   //and prints it
    Here he says that ptr first gets incremented and points to cell 8, and then the value gets printed. Which, as we know from the semantics of the postincrement operator, is just not true.

    Once again, that will behave as if you had typed:
    Code:
      printf("Did I miss out my lucky number %d?!\n", *ptr);
      ptr++;
    as two separate statements.

    Now, the += operator does NOT behave like the postincrement operator. The += operator will return the new value of ptr after the addition occurs. So you were correct the first time, you just did the addition wrong.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so += first adds then returns the value
    ++ return the value then increments

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. where is my mistake in undersanding the order of this code..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 10-08-2008, 08:26 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM