Where is my mistake following this code

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Code Mistake
AI Thread Summary
The discussion revolves around a C programming code snippet that manipulates a pointer to an array of integers. The main point of contention is the line where the pointer is incremented by 4 to access the 8th element of the array. The comment suggests that the pointer directly points to the 8th element after the increment, but this is misleading. The pointer actually points to the 7th element before the increment, and the comment fails to clarify that the pointer's value is updated before dereferencing it. This misunderstanding highlights the importance of accurately commenting on pointer arithmetic and the implications of pointer increments in C programming. The thread encourages a deeper examination of pointer manipulation to ensure clarity and correctness in understanding how pointers interact with array indices.
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;
}
 
Technology news 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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top