Segfaults happen when you use invalid pointers. If your code has a problem, it's in the "some code here" part. Note though that s=N happens only once before the loop starts. Any changes to N afterwards has no effect on s.
Use the debugger. Step through the code one line at a time. Look at the values of variables as you step. Nobody here is going to be able to help much unless you post something that is more complete.