I'm trying to make a simple prime number generator for bigger program. It needs two numbers (n and c) and needs to edit them. So I decided to put the numbers in pointers, and just give the adress two the pointers. After about a half-houre of fumbling with *'s and &'s my eye's need a break. Here's a copy of the code without the pointerstuff on it:

#include <stdio.h>
int nextprime(int n, int c);

int main() {
  int n = 0, c = 0;
  printf("%d, ", nextprime(n, c));
  printf("%d, ", nextprime(n, c));
  printf("%d, ", nextprime(n, c));
  printf("%d, ", nextprime(n, c));

  return 0;
}

int nextprime(int n, int c) {
/* This function will return the next prime number. Simple set n and c at 0 and 
   call the function whenever you need the next prime number. */
   if(n == 0) {
      n++;
      return 2;
   }
   if(c == 0) {
      c++;
      return (6*n)-1;
   }
   if(c == 1) {
      n++;
      c--;
      return (6*n)+1;
   }
}

Good luck and thanks!:P

Recommended Answers

All 8 Replies

Good luck and thanks!

I think it would be better to show your code with the pointer stuff so that someone can help you understand where you went wrong than to show your code without the pointer stuff and hope somebody does all of your work for you.

commented: Indeed +36

I think it would be better to show your code with the pointer stuff so that someone can help you understand where you went wrong than to show your code without the pointer stuff and hope somebody does all of your work for you.

Sure thing. I've tried changing the "&" to "*" and other combinations as well. I'm trying to pass the adress of n and c, so my function can modify them.

#include <stdio.h>
int nextprime(int n, int c);

int main() {
  int n = 0, c = 0;
  printf("%d, ", nextprime(&n, &c));
  printf("%d, ", nextprime(&n, &c));
  printf("%d, ", nextprime(&n, &c));
  printf("%d, ", nextprime(&n, &c));

  return 0;
}

int nextprime(int n, int c) {
/* This function will return the next prime number. Simple set n and c at 0 and 
   call the function whenever you need the next prime number. */
   if(&n == 0) {
      &n++;
      return 2;
   }
   if(&c == 0) {
      &c++;
      return (6*(&n))-1;
   }
   if(&c == 1) {
      &n++;
      &c--;
      return (6*(&n))+1;
   }
}

A quick guide on passing variables by reference, using pointers

Consider this function:

void padd(int *p, int b)  // create a pointer to an integer variable (parameter)
{
    *p += b;  // add the value in 'b' to the number
}

You'll have to use the function like this:

int var = 3;
padd(&var, 5); // pass the variable by reference

printf("%d\n", var); // will print '8' on the screen

Hope this helps :)

You are passing the address of n and c. That means nextprime should expect pointers:

int nextprime(int *n, int *c);

Then inside nextprime, n and c are already pointers so you need to dereference them to get to the value. n and c gives you the address, *n and *c gives you the value at that address.

You can almost fix things inside nextprime by changing the &'s to *'s, but because of precedence *n++ and other expressions like it won't do what you want. Instead of incrementing the value at the address pointed to by n, it increments the address of n and evaluates to the value at that new address.

To get the right precedence you need to use parentheses: (*n)++; . Dereference n, then increment that value. Everything you have in main is perfect, but a '\n' at the very end couldn't hurt. ;)

#include <stdio.h>
int nextprime(int *n, int *c);

int main() {
  int n = 0, c = 0;
  printf("%d, ", nextprime(&n, &c));
  printf("%d, ", nextprime(&n, &c));
  printf("%d, ", nextprime(&n, &c));
  printf("%d\n", nextprime(&n, &c));

  return 0;
}

int nextprime(int *n, int *c) {
/* This function will return the next prime number. Simple set n and c at 0 and 
   call the function whenever you need the next prime number. */
   if(*n == 0) {
      (*n)++;
      return 2;
   }
   if(*c == 0) {
      (*c)++;
      return (6*(*n))-1;
   }
   if(*c == 1) {
      (*n)++;
      (*c)--;
      return (6*(*n))+1;
   }
}

Here's a more simplified example:

#include <stdio.h>

int increase(int a, int b) {
  a++;
  b++;
}

int main() {
  int num1 = 10;
  int num2 = 12;
  increase(num1, num2);
  printf("10 increased by 1 is %d, 12 increased by 1 is %d\n", num1, num2);

  return 0;
}

I want a & b to stay increased so I do something like this,

#include <stdio.h>

int increase(int a, int b) {
  *a++;
  *b++;
}

int main() {
  int *num1 = 10;
  int *num2 = 12;
  increase(num1, num2);
  printf("10 increased by 1 is %d, 12 increased by 1 is %d\n", *num1, *num2);

  return 0;
}

I've tried multiple variations like the one above, and they all don't work. Can someone show me why it dosent work and what would work? Thanks

whoa two post as I was typing the last one. Thanks guy's, thats loads of information:)

Here is a quick test to see how the two expressions work:

#include <stdio.h>

int main()
{
    int a[] = {2, 1};
    int *p = a;

    printf("%d\n", (*p)++);
    printf("%d\n", *p);
    printf("%d\n", *p++);
    printf("%d\n", *p);

    return 0;
}

Add '*' and change it to void and then it should work:

[B]void[/B] increase(int [B]*[/B]a, int [B]*[/B]b) {
  [B]([/B]*a[B])[/B]++;
  [B]([/B]*b[B])[/B]++;
}

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.