2
#include <stdlib.h>
#include <stdio.h>
void roll_three(int* one, int* two, int* three)
{

  int x,y,z;
  x = rand()%6+1;
  y = rand()%6+1;
  z = rand()%6+1;

  one = &x;
  two = &y;
  three = &z;
  printf("%d %d %d\n", *one,*two,*three);  
}
int main()
{
  int seed;
  printf("Enter Seed: ");
  scanf("%d", &seed);
  srand(seed);
  int x,y,z;
  roll_three(&x,&y,&z);
  printf("pai: %d %d %d\n", x,y,z);
  if((x==y)&&(y==z))
    printf("%d %d %d Triple!\n",x,y,z);
  else if((x==y)||(y==z)||(x==z))
    printf("%d %d %d Double!\n",x,y,z);
  else
    printf("%d %d %d\n",x,y,z);
  return 0;

}

This the terminal, I type 123 for the seed. However, the printf in roll_three and the printf in main give me different output? Why *one and x are different?

1
  • the function srand() is expecting an unsigned int so suggest main() start with: unsigned seed; printf("Enter Seed: "); scanf("%u", &seed); srand(seed); Commented Feb 27, 2016 at 21:06

4 Answers 4

6

The problem is here:

one = &x;
two = &y;
three = &z;

Since one, two, and three are pointers you've changed what they point to, but now they no longer point to main's x, y and z. It is similar to this...

void foo(int input) {
    input = 6;
}

int num = 10;
foo(num);
printf("%d\n", num);  // it will be 10, not 6.

Instead you want to change the value stored in the memory they point to. So dereference them and assign them the new value.

*one = x;
*two = y;
*three = z;

You can even eliminate the intermediate values.

*one   = rand()%6+1;
*two   = rand()%6+1;
*three = rand()%6+1;
Sign up to request clarification or add additional context in comments.

Comments

2

In roll_three, you need to change the following:

one = &x;
two = &y;
three = &z;

To:

*one = x;
*two = y;
*three = z;

The first version just points to them to the local variables. The corrected version updates the values in the caller.

4 Comments

But when I make the int x,y,z in roll_three as a global variable, write behind the #include<stdio.h>, there are still different? I know "*one = x;" is correct, but why "one = &x;" is not correct.
If you make them global, then you have to remove the local declarations from both roll_three and main. Otherwise the local definitions will mask the global ones. But you don't really want to make them global.
I rename that x, y,z as xx, yy, zz and change the all x, y,z in roll_three as xx yy zz. There still occur some problem, I will post that in another comment, can you check that for me?
I'm not sure what you're doing at this point, but if you post the code I can take a look.
1

Why *one and x are different?

one = &x;

should be

*one = x;

The way you did it ( one = &x ) is wrong, because you assign pointer one to the address of local variable x which no longer exists after function roll_three.

You function should look like this:

void roll_three(int* one, int* two, int* three)
{

    int x,y,z;
    x = rand()%6+1;
    y = rand()%6+1;
    z = rand()%6+1;

    *one = x;
    *two = y;
    *three = z;
    printf("%d %d %d\n", *one,*two,*three);  
}

2 Comments

But when I make the int x,y,z in roll_three as a global variable, write behind the #include<stdio.h>, there are still different? I know "*one = x;" is correct, but why "one = &x;" is not correct.
@HanslenChen - it can't be; unless you declared x,y,z as global variables, and then forgot to delete the local variables x,y,z in main
0

In your roll_three function,

void roll_three(int* one, int* two, int* three)
{
  int x,y,z;
  x = rand()%6+1; // local variable x will have a value
  y = rand()%6+1;
  z = rand()%6+1;

  one = &x; // variable one is assigned with local variable x's address
            // so *one equals to x inside the function.
            // However, variable one supposed is the variable x's address in 
            // the main function, but now it is changed to the local x's address,
            // the main function variable x's value can't be updated as expected.
            // *one = x is the way you want to go.
  two = &y;
  three = &z;
  printf("%d %d %d\n", *one,*two,*three);  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.