Jump to content

C - simple prime number generator, segfault

Sauron
Go to solution Solved by Guest,

you forgot & in scanf.

Hey guys!

 

I was playing aroud with some c, since I hadn't really used it in the past and now I have a course about it.

 

I decided to make a prime number generator, and I was able to get what I wanted with little effort. However, I then tried to make a separate generator function to improve modularity. BUT, I get segfault. Any ideas?
 

MY CODE:

#include <stdio.h>#define MAX_INDEX 150000void generate(int array[MAX_INDEX], int nums){  int index, iter, i;  int primes[nums], num;  char isprime;  isprime = 1;  index, iter = 0;  num =2;  while(index < nums)    {      for(iter = 0; iter < index; iter++)        {          if((num % primes[iter]) == 0)            {              isprime = 0;              break;            }        }      if(isprime)        {          primes[index] = num;          index++;        }      isprime = 1;      num++;    }  array = primes;  printf("Generated %d prime numbers\n", nums);  return;}int main(){  int primes[MAX_INDEX], index;  if(scanf("%d", index) == 1 && index <= MAX_INDEX)    {      generate(primes, index);    }  return 0;}

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

If you use void to create it than there is no return.

Link to comment
Share on other sites

Link to post
Share on other sites

you forgot & in scanf.

Link to comment
Share on other sites

Link to post
Share on other sites

If you use void to create it than there is no return.

 

you forgot & in scanf.

 

OH you're right lol ^^ thanks!

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

OH you're right lol ^^ thanks!

I thought your eye can see everything :)

 

photo-42239.gif?_r=1421266775

Link to comment
Share on other sites

Link to post
Share on other sites

I thought your eye can see everything :)

 

See != notice ^^

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×