The below code works fine for the moment and multiply all the numbers to give the answer 6 in this example, just want to understand the while statement is not working if I use (arrn[i] != '\0') or (arr) or (arr != 0) it gives wrong output.
int perm(int arrn[]) { int i =0; int z; int t = 1; int result =1; while(arrn[i] < 9){ z = arrn[i]; t = t * z; //printf("%d", z); i++; } printf("%d", t); }
int main() {
int list1[] = {1,2,3};
perm(list1);
}
Once i is 3 you're reading past the end of the array. Neither of your two conditions prevent that from happening. Doing so triggers undefined behavior in your code.
When you have undefined behavior, there is no guarantee what your code will do. It might crash, it might output strange results (as the case of your alternate definition does), or it might appear to work properly (as it does in your original code).
You need to pass in the size of the array to the function, then use that to determine when to stop iterating.