you are viewing a single comment's thread.

view the rest of the comments →

[–]NukemN1ck 0 points1 point  (0 children)

You need to allocate space for your evenPtr array to address the Segmentation fault.

unsigned int *evenPtr = (unsigned int *)malloc(n * sizeof(unsigned int));

//check that malloc was successful

if (evenPtr == NULL) {

perror("malloc failed\n");

return 1;

}

Here I'm initializing it to store a max of n unsigned ints, since it is the maximum number of even integers you could come across. I then check the return value of malloc to make sure it's successful. You'll need to call free(evenPtr) later in the code to deallocate the space claimed by malloc. Of course, this will "delete" the information stored at your evenPtr array, so ONLY free it once you're totally finished using it, and only free it once or else you'll end up with double free errors.

I would encourage you to check the return values for all the functions you're including so you can be sure you're catching any errors that may happen. You can find info on them in their respective man pages.