c - What dos count[i]++ mean -


hello came across explanation finding first non repeating number in array. http://www.geeksforgeeks.org/given-a-string-find-its-first-non-repeating-character/ in explanation , did not understand means

count[*(str+i)]++;  or count[index]++; 

can please me understand

short version:

count[index]++ shorthand count[index] = count[index] + 1.

long version:

the expression i++ evaluates current value of i, , side effect increments value stored in i 1. given code

int = 1, b; b = a++; printf( "a = %d, b = %d\n", a, b ); 

the output be

a = 2, b = 1 

there corresponding prefix version ++i; in case, expression evaluates i+1 , side effect increments i 1,

int = 1, b; b = ++a; printf( "a = %d, b = %d\n", a, b ); 

gives output

a = 2, b = 2 

there corresponding prefix , postfix -- operators subtract 1.

note expressions like

i++ * i++ = i++ a[i] = i++ printf( "%d %d", i++, i++ ) 

all have undefined behavior, , not give consistent results across platforms (or across builds on same platform). few exceptions, c not guarantee expressions evaluated in specific order, nor guarantee side effects applied after expression has been evaluated.


Comments