if its a problem from codechef then make sure to scan testcase that’s may be one of the reason for runtime error.
2ndly try to initalize that “i” pointer to zero again before the while loop. Because some times the pointers stores the garbage values may be this works for you