Hanslen Chen
Hanslen Chen

Reputation: 440

Why the same variable print the different output?

#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?

Upvotes: 2

Views: 112

Answers (4)

Schwern
Schwern

Reputation: 165416

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;

Upvotes: 6

Kaizhe Huang
Kaizhe Huang

Reputation: 1006

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);  
}

Upvotes: 0

tdao
tdao

Reputation: 17713

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);  
}

Upvotes: 1

Tom Karzes
Tom Karzes

Reputation: 24102

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.

Upvotes: 2

Related Questions