【问题标题】:Why the same variable print the different output?为什么相同的变量打印不同的输出?
【发布时间】:2016-02-26 22:33:36
【问题描述】:
#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;

}

这是终端,我输入 123 作为种子。但是,roll_three 中的 printf 和 main 中的 printf 给了我不同的输出?为什么 *one 和 x 不同?

【问题讨论】:

  • 函数srand() 期待unsigned int 所以建议main() 开头:unsigned seed; printf("Enter Seed: "); scanf("%u", &amp;seed); srand(seed);

标签: c pointers


【解决方案1】:

问题出在这里:

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

由于onetwothree 是指针,您已经更改了它们指向的内容,但现在它们不再指向mainxyz .和这个差不多……

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

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

相反,您想更改存储在它们指向的内存中的值。所以取消引用它们并为它们分配新值。

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

您甚至可以消除中间值。

*one   = rand()%6+1;
*two   = rand()%6+1;
*three = rand()%6+1;

【讨论】:

    【解决方案2】:

    roll_three 中,您需要更改以下内容:

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

    收件人:

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

    第一个版本只是将它们指向局部变量。更正后的版本会更新调用者中的值。

    【讨论】:

    • 但是当我把roll_three中的int x,y,z做为全局变量,写在#include后面,还是有区别?我知道“*one = x;”是正确的,但为什么 "one = &x;"不正确。
    • 如果将它们设为全局,则必须从 roll_threemain 中删除本地声明。否则,本地定义将掩盖全局定义。但你并不是真的想让它们全球化。
    • 我将 x、y、z 重命名为 xx、yy、zz 并将 roll_three 中的所有 x、y、z 更改为 xx yy zz。还是出现了一些问题,我会在另一条评论中发布,你能帮我检查一下吗?
    • 我不确定你现在在做什么,但如果你发布代码我可以看看。
    【解决方案3】:

    为什么 *one 和 x 不同?

    one = &x;
    

    应该是

    *one = x;
    

    你这样做的方式(one = &amp;x)是错误的,因为你将指针one分配给局部变量x的地址,在函数roll_three之后不再存在。

    你的函数应该是这样的:

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

    【讨论】:

    • 但是当我把roll_three中的int x,y,z做为全局变量,写在#include后面,还是有区别?我知道“*one = x;”是正确的,但为什么 "one = &x;"不正确。
    • @HanslenChen - 不可能;除非你将x,y,z声明为全局变量,然后忘记删除main中的局部变量x,y,z
    【解决方案4】:

    在你的 roll_three 函数中,

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

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 2020-02-11
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2019-03-01
      • 2019-01-28
      • 1970-01-01
      • 2016-07-01
      相关资源
      最近更新 更多