Where is my mistake following this code

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Code Mistake
Click For Summary
SUMMARY

The discussion centers on a C programming code snippet that manipulates a pointer to an integer array. The user seeks clarification on the pointer arithmetic, specifically the line where the pointer is incremented by 4 to access the 8th element of the array. The confusion arises from the use of the expression *(ptr+=4), which modifies the pointer before dereferencing it, potentially leading to misunderstandings about the pointer's current position and the value being printed.

PREREQUISITES
  • Understanding of C programming syntax and semantics
  • Knowledge of pointer arithmetic in C
  • Familiarity with arrays in C
  • Basic debugging techniques in C programming
NEXT STEPS
  • Study C pointer arithmetic in detail
  • Learn about the implications of modifying pointers in expressions
  • Explore debugging tools for C, such as GDB
  • Review best practices for pointer usage in C programming
USEFUL FOR

C programmers, computer science students, and anyone looking to deepen their understanding of pointer manipulation and array handling in C.

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?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
2K
Replies
7
Views
5K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K