Post decrement during conditon in C

I did a search and Ive not found anything in my query. I am currently debugging the C optimizer, and the corresponding code is as follows:

while( x-- )
array[x] = NULL;

What should happen in this case? And should the result of this logic be consistent across all compilers?

Assume that the initial value of x in this case is 5.

The problem is that the program worked, I understand that it is caused by a reference to a negative array element.

Any help would be appreciated.

+3
source share
3 answers

x -1 ( , x ), array[-1] . - array[0]. .

, , . array[x] , array[-1].

, , , , unsigned .

unsigned x;
int a[5];

for (x = 5; x-- > 0; )
  a[x] = 0;

. ( , x >= 0 .) , - , - , . (, ).

+6

x 5, :

array[4] = NULL;
array[3] = NULL;
array[2] = NULL;
array[1] = NULL;
array[0] = NULL;

x , x -1; .

+1

, x while ( ). , x -1, while ( ). while array x .

0

All Articles