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

All*_*oto 1 c arrays pointers

我在使用指针时遇到了一些麻烦.

它的要点是,我试图在一个函数中定义指针,并在我的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);
}
Run Code Online (Sandbox Code Playgroud)

然而,在程序问我三个整数后,它没有做任何事情就崩溃了.

第一个函数通过它自己正常工作(通过使它成为没有参数的主函数并打印指针值来测试它),即:

printf ("%d", *x);
Run Code Online (Sandbox Code Playgroud)

所以我猜这些值只是没有从一个函数传递到下一个函数.我已经尝试过编写第一个和第二个函数的各种方法,但似乎没有任何工作.

我得到的最好的是让程序不会崩溃,但打印的价值是我之前输入的.

任何想法如何做到这一点?

Tim*_*nes 5

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

1)您正在返回变量的本地地址 a,b并且c:

*x = &a; // This line says follow the 'x' pointer, and set the value there to
         // the address of 'a'
Run Code Online (Sandbox Code Playgroud)

由于a是在本地定义的(即在函数内部),一旦函数返回,该地址就无效.

你可能意味着:

*x = a; // Follow the 'x' pointer, and set the value there to the value of 'a'
Run Code Online (Sandbox Code Playgroud)

2)你没有传递指针pntloc()(你的编译器应该警告你这个)

int x, y, z;
pntloc(x, y, z); // The passes the VALUES of x, y and z
Run Code Online (Sandbox Code Playgroud)

你可能意味着:

pntloc(&x, &y, &z); // Pass the ADDRESSES of x, y and z
Run Code Online (Sandbox Code Playgroud)

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

您可以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);
}
Run Code Online (Sandbox Code Playgroud)

请注意,&已在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'.
Run Code Online (Sandbox Code Playgroud)

因为我们有传递给函数的指针:

void pntloc (int *x, int *y, int *z){  // Three pointers to ints
Run Code Online (Sandbox Code Playgroud)

我们可以直接将它们传递给我们scanf(),并且不需要(也不应该)&在我们这样做时使用.

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

 return *x, *y, *z; 
Run Code Online (Sandbox Code Playgroud)

我不认为这个退货声明正在做你认为的那样.请记住,C只允许函数返回一个值.

但你问为什么要编译?好吧,这就是正在发生的事情 - 但如果令人困惑,请随意忽略这一点:逗号运算符从左到右进行评估,随着时间的推移丢弃左手结果.所以,你的return语句相当于:

*x;
*y;
return *z;
Run Code Online (Sandbox Code Playgroud)

当左手语句具有副作用,并且您想要在一行上写入所有内容时,逗号运算符很有用.在我看来,它使代码更难阅读,但有一两种情况可以使事情更清晰.对于初学者,我建议使用以下规则:仅在函数的圆括号内使用逗号.

由于您在调用函数时没有使用函数的返回值:

pntloc(&x,&y,&z);
Run Code Online (Sandbox Code Playgroud)

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