Where is my mistake following this code

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Code Mistake
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 replies · 2K views
transgalactic
Messages
1,386
Reaction score
0
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 %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;
}
 
on Phys.org
Do you notice anything strange about the line

Code:
printf("Now skip the next 4 to point to the 8th: %d.\n", 
          *(ptr+=4));                                                            //ptr points to cell[8]=9

In particular, is your comment on this line really correct?