0

I referred this link Pointer expressions: *ptr++, *++ptr and ++*ptr to understand pointer arithmetic.

Why the below code is going to infinite loop?

int main(){
 int a[4] ={1,2,3,4};
 int *ptr = a;
 while (++*ptr){ 
    printf("%d",*ptr);
 }
}
Shaeldon
  • 873
  • 4
  • 18
  • 28
alisha
  • 39
  • 8
  • 2
    Use a debugger to inspect the value of ptr and of *ptr at each iteration. – ClickRick Dec 05 '14 at 11:10
  • 1
    Please use the edit link under your question and format your code snipper properly. – peter.hrasko.sk Dec 05 '14 at 11:10
  • 1
    When do you expect the value of ++*ptr to be equal to 0, thus ending the loop? – Daniel Daranas Dec 05 '14 at 11:12
  • Change while condition to "++*ptr != null".... – Amol Bavannavar Dec 05 '14 at 11:14
  • @AmolBavannavar That code is not correct... – Lundin Dec 05 '14 at 12:12
  • As per the link mentioned in question.++*ptr will cause crash if points to string .But I have declared an int array. – alisha Dec 05 '14 at 12:16
  • To sum up, here are the three things you asked about: *ptr++ // effectively dereferences the pointer, then increments the pointer *++ptr // effectively increments the pointer, then dereferences the pointer ++*ptr // effectively dereferences the pointer, then increments dereferenced value---------------------------------------------------------------------------------------------------------------------------This is mentioned in the above link.Correct me if I am going wrong .My intention was to cross check these things. – alisha Dec 05 '14 at 12:18

5 Answers5

2

Your code does not work for two reasons:

  • ++*ptr increments the number, not the pointer
  • You are iterating an array instead of a C string

You can iterate a C string using while (*ptr++) expression for the loop condition. This little trick works for C strings because they are null terminated. In order to make it work for arrays you would need to put zero at the end of the array, and agree to not use zeros anywhere else in the array:

int a[4] ={1,2,3,4, 0};
int *ptr = a;
int last;
while (last = *ptr++) { 
    printf("%d", last);
}

Note that since we are incrementing the pointer in the header of the loop, we should store the last value pointed to by the pointer in a separate variable. Otherwise we'd skip over one array element.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

in loop condition value is 0 that time only loop terminated but you just increment the first position of the array value so zero value will not occur.

Rajalakshmi
  • 681
  • 5
  • 17
0

The issue is in the following line. Your while will break only if the value of ++*ptr is false or 0. But it never becomes 0 or false.

++*ptr

so while(NON ZERO) will result in infinite loop.
Sridhar DD
  • 1,972
  • 1
  • 10
  • 17
  • 1
    Technically, this is not right: `++*ptr` becomes zero when the number it points to overflows. It takes about 4 bn iterations on a 32-bit computer, though, so the result looks like an infinite loop. – Sergey Kalinichenko Dec 05 '14 at 13:08
  • `++*ptr` **will** become zero. – ClickRick Dec 05 '14 at 13:28
  • @dasblinkenlight ClickRick That is not right either, the value cannot become 0 because C doesn't define signed overflow. So technically Sridhar is correct. The value will never become 0, as long as we have defined behavior. – 2501 Dec 05 '14 at 14:21
  • @2501 The fact that the behavior is undefined makes it incorrect to rely on the value becoming zero after an overflow, but it is incorrect to classify the loop as infinite either, because once undefined behavior is inevitably triggered by this program, all bets are off. Of course on most hardware systems in use today the value quietly becomes zero. – Sergey Kalinichenko Dec 05 '14 at 14:41
  • @dasblinkenlight:: please take a look at this to confirm whether your down vote is right or not http://stackoverflow.com/questions/3108022/incrementing-an-integer-value-beyond-its-integer-limit-c-sharp – Sridhar DD Dec 06 '14 at 05:09
  • @SridharDD I left a comment, but I did not vote this answer one way or the other. The answer says essentially the same thing as my comment, though. – Sergey Kalinichenko Dec 06 '14 at 11:34
0

The loop is "infinite" because the condition in the while statement is always true until you hit the maximum value int can hold and after that you get undefined behavior.

What happens in the line is:

while (++*ptr)

First the pointer is dereferenced *ptr, obtaining the value of the first element in the array a, then that value is incremented by one. And then that resulting value is evaluated, giving the true result.

The same happens on every loop, the pointer ptr keeps pointing to the same element, the first one ptr == &a[0] , and keeps incrementing the value of that element by one a[0] = a[0]+1.

2501
  • 25,460
  • 4
  • 47
  • 87
-1

You are incrementing the value of the corresponding 0th position. So the starting position value of array is incremented. So the loop executing infinitely. Make the while loop as following.

while(*(++ptr))
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31