指针作为调用scanf的函数的参数

指针作为调用scanf的函数的参数,c,arrays,pointers,C,Arrays,Pointers,我的指针有点问题 其要点是,我试图在一个函数中定义指针,并在main中调用该函数以使用这些指针。 我的任务的具体说明如下: 编写两个函数,一个从键盘读取三个数字 还有一个打印这三个数字的信息 输入函数 编写一个包含三个整数指针参数的函数,然后 要求用户输入三个整数。在中输入的值 键盘应读入指针中存储的地址 参数 注意:回想一下scanf需要一个变量的地址 指针存储地址 打印值 编写第二个名为a2question2的函数,该函数没有返回值,并且 没有参数。函数应该声明三个整数变量 然后使用输入函数

我的指针有点问题

其要点是,我试图在一个函数中定义指针,并在main中调用该函数以使用这些指针。 我的任务的具体说明如下:

编写两个函数,一个从键盘读取三个数字 还有一个打印这三个数字的信息

输入函数

编写一个包含三个整数指针参数的函数,然后 要求用户输入三个整数。在中输入的值 键盘应读入指针中存储的地址 参数

注意:回想一下scanf需要一个变量的地址 指针存储地址

打印值

编写第二个名为a2question2的函数,该函数没有返回值,并且 没有参数。函数应该声明三个整数变量 然后使用输入函数将值读入这些变量。 然后,函数应打印总和、平均值、乘积和 这些数字中最小和最大的

以下是我到目前为止的情况:

int pntloc (int *x, int *y, int *z){
  int a = 0;
  int b = 0;
  int c = 0;

  printf("Please enter integer #1: ");
  scanf ("%d", & a);
  printf ("Please enter integer #2: ");
  scanf ("%d", & b);
  printf("Please enter integer #3: ");
  scanf ("%d", & c);

  *x = &a;
  *y = &b;
  *z = &c;

  return *x, *y, *z;
}

// Fourth function
main (){
  int x, y, z;
  pntloc(x, y, z);

  int sum = 0;
  int average = 0;
  int product = 0;
  int smallest = 0;
  int largest = 0;

  printf ("%d", x);
}
然而,在程序要求我输入三个整数后,它什么也不做就崩溃了

第一个函数通过自我测试工作良好(通过使其成为无参数的主函数并打印指针值进行测试),即:

所以我猜这些值不是从一个函数传递到下一个函数。我尝试了各种编写第一个和第二个函数的方法,但似乎没有任何效果

我得到的最好结果是让程序不崩溃,但打印的值与我之前输入的值相差甚远

有什么办法吗

*x = &a;
*y = &b;
*z = &c;
会造成混乱和死亡!a b和c是局部变量,因此将x y和z的内容设置为从定义了a b和c的函数返回后将无效的地址。 也许你的意思是:

*x = a;
*y = b;
*z = c;
会造成混乱和死亡!a b和c是局部变量,因此将x y和z的内容设置为从定义了a b和c的函数返回后将无效的地址。 也许你的意思是:

*x = a;
*y = b;
*z = c;
总的来说()

在pntloc()中将*x作为参数传递时,调用函数后不能更改*x的值

pntloc()不需要返回它们,返回0或1就足够了。

在main()中

在pntloc()中将*x作为参数传递时,调用函数后不能更改*x的值


pntloc()不需要返回它们,返回0或1就足够了。

您的程序可能会因为两个错误而崩溃:

1) 返回变量的本地地址
a
b
c

*x = &a; // This line says follow the 'x' pointer, and set the value there to
         // the address of 'a'
由于
a
是在本地(即函数内部)定义的,因此一旦函数返回,该地址就无效

你的意思可能是:

*x = a; // Follow the 'x' pointer, and set the value there to the value of 'a'
2) 您没有传递指向
pntloc()
的指针(您的编译器应该对此发出警告)

你可能是说:

pntloc(&x, &y, &z); // Pass the ADDRESSES of x, y and z

其他一些不会导致崩溃的改进:

通过不使用局部变量,可以大幅缩短pntloc():

void pntloc (int *x, int *y, int *z){
  printf("Please enter integer #1: ");
  scanf ("%d", x);
  printf ("Please enter integer #2: ");
  scanf ("%d", y);
  printf("Please enter integer #3: ");
  scanf ("%d", z);
}
请注意,
&
已在
scanf()调用中删除。你在评论中问到了这个问题,所以这里有更多的解释:
&x
说“x的地址”,但是当你有一个指针时,你已经有了一个地址。一个简单的例子:

int a;       // 'a' is an integer variable
int *b = &a; // 'b' is a pointer to the integer variable 'a'

scanf("%d",&a); // This statement reads an integer into 'a'.
                // We pass it the address of 'a', which is written &a
scanf("%d",b);  // This statement also reads an integer into 'a'.
                // We pass it the address of 'a', which is stored 
                // in the pointer 'b'.
因为我们已经向函数传递了指针:

void pntloc (int *x, int *y, int *z){  // Three pointers to ints
我们可以直接将它们传递到
scanf()
,这样做时就不需要(也不应该)使用
&

请注意,我还删除了return语句:

 return *x, *y, *z; 
我不认为这个返回声明做了你认为它是。请记住,C只允许函数返回一个值

但你会问,它为什么要编译?好吧,这里是正在发生的事情-但是如果这一点令人困惑,请随意忽略它:从左到右计算结果,在进行时丢弃左手结果。因此,您的返回语句相当于:

*x;
*y;
return *z;
如果左侧语句有副作用,并且希望将所有内容都写在一行上,则逗号运算符非常有用。在我看来,它使代码更难阅读,但有一两种情况下,它使事情更干净对于初学者,我推荐以下规则:只在函数的圆括号内使用逗号。

由于调用函数时未使用该函数的返回值:

pntloc(&x,&y,&z);

我完全删除了返回,并将返回类型设置为
void

您的程序可能由于两个错误而崩溃:

1) 返回变量的本地地址
a
b
c

*x = &a; // This line says follow the 'x' pointer, and set the value there to
         // the address of 'a'
由于
a
是在本地(即函数内部)定义的,因此一旦函数返回,该地址就无效

你的意思可能是:

*x = a; // Follow the 'x' pointer, and set the value there to the value of 'a'
2) 您没有传递指向
pntloc()
的指针(您的编译器应该对此发出警告)

你可能是说:

pntloc(&x, &y, &z); // Pass the ADDRESSES of x, y and z

其他一些不会导致崩溃的改进:

通过不使用局部变量,可以大幅缩短pntloc()

void pntloc (int *x, int *y, int *z){
  printf("Please enter integer #1: ");
  scanf ("%d", x);
  printf ("Please enter integer #2: ");
  scanf ("%d", y);
  printf("Please enter integer #3: ");
  scanf ("%d", z);
}
请注意,
&
已在
scanf()调用中删除。你在评论中问到了这个问题,所以这里有更多的解释:
&x
说“x的地址”,但是当你有一个指针时,你已经有了一个地址。一个简单的例子:

int a;       // 'a' is an integer variable
int *b = &a; // 'b' is a pointer to the integer variable 'a'

scanf("%d",&a); // This statement reads an integer into 'a'.
                // We pass it the address of 'a', which is written &a
scanf("%d",b);  // This statement also reads an integer into 'a'.
                // We pass it the address of 'a', which is stored 
                // in the pointer 'b'.
因为我们已经向函数传递了指针:

void pntloc (int *x, int *y, int *z){  // Three pointers to ints
我们可以直接将它们传递到
scanf()
,这样做时就不需要(也不应该)使用
&

请注意,我还删除了return语句:

 return *x, *y, *z; 
我不认为这个返回声明做了什么