Where is my mistake following this code

In summary, the conversation is discussing how to change the index of a pointer in an array. The example code increments the pointer and shows how it points to different elements in the array. The conversation also mentions the importance of being careful with pointer arithmetic.
  • #1
transgalactic
1,395
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
  • #2
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?
 
  • #3


Your mistake is in the line where you increment the pointer by 2 (ptr+=2). This will make the pointer point to the 6th element instead of the 8th element as you intended. To skip 4 elements and point to the 8th element, you should increment the pointer by 3 (ptr+=3) instead. Additionally, when printing the skipped elements, you should use *(ptr+3) and *(ptr+4) instead of *(ptr+1) and *(ptr+2).
 

Related to Where is my mistake following this code

1. What is the purpose of this code?

The purpose of the code is not specified, so it is difficult to determine if there is a mistake. It is important to clearly define the purpose of your code to ensure it is written correctly.

2. Is there a specific error message or result that is causing confusion?

If there is an error message, it can provide helpful clues to identify the mistake. Make sure to read and understand the error message, as it can point to the line or section where the mistake may be located.

3. Have you checked for syntax errors?

Syntax errors are common mistakes in coding and can cause the code to not run properly. Make sure to check for missing or incorrect punctuation, brackets, and quotations.

4. Are all variables and functions properly defined and used?

Undefined or incorrectly used variables and functions can cause errors in the code. Make sure all variables and functions are properly defined and used in the correct context.

5. Have you tested the code with different inputs?

Testing the code with different inputs can help identify where the mistake may be occurring. Make sure to test edge cases and different scenarios to ensure the code is functioning correctly.

Similar threads

  • Programming and Computer Science
Replies
2
Views
987
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
7
Views
4K
  • Programming and Computer Science
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
11K
  • Programming and Computer Science
Replies
4
Views
771
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top