where is my mistake following this code..

This is a discussion on where is my mistake following this code.. within the C Programming forums, part of the General Programming Boards category; next to every line i commented on what i think it does
where is my mistake in understanding of changing ...

where is my mistake following this code..

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

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.

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

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.

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.