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

标签 c arrays pointers

我在使用指针时遇到了一些问题。

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

Write two functions, one that reads three numbers from the keyboard and one that prints some information about these three numbers.

Input Function

Write a function that has three integer pointer parameters, and that asks the user to enter three whole numbers. The values entered at the keyboard should be read into the addresses stored in the pointer parameters.

Note: recall that scanf requires the address of a variable and that pointers store addresses.

Printing Values

Write a second function called a2question2, with no return value and no parameters. The function should declare three integer variables and then use your input function to read values into these variables. The function should then print the sum, the average, the product, and the smallest and largest of these numbers.

这是我目前所拥有的:

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

但是,在程序向我询问三个整数后,它什么也没做就崩溃了。

第一个函数本身运行良好(通过将其作为不带参数的主函数进行测试并打印指针值)即:

printf ("%d", *x);

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

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

有什么办法吗?

最佳答案

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

1) 你正在返回变量的本地地址 a, b and 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()(您的编译器应该警告您这一点)

int x, y, z;
pntloc(x, y, z); // The passes the VALUES of x, y and z

你的意思可能是:

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; 

我认为这个 return 语句并没有按照您的想法进行。请记住,C 语言只允许一个函数返回一个值。

但是你问为什么它能编译?好吧,这就是正在发生的事情 - 但如果它令人困惑,请随意忽略这一点:comma operator从左到右计算,丢弃左边的结果。因此,您的返回语句等效于:

*x;
*y;
return *z;

逗号运算符在左侧语句有副作用且您想将所有内容写在一行时很有用。在我看来,它使代码更难阅读,但在一两种情况下它会使代码更清晰。 对于初学者,我建议遵循以下规则:仅在函数的圆括号内使用逗号。

因为您在调用函数时没有使用函数的返回值:

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

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

关于c - 指针作为调用 scanf 的函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17181371/

相关文章:

java - 从文本文件中获取数字列表,转换为单个字符串,然后用空格或逗号将该列表拆分为两个数组列表

javascript - 代码错误 - 分割字符串并返回每个单词的长度 JS

Java - 避免使用父类(super class)对象数组进行强制转换

c - 按位补码运算符返回的数据类型是什么?

c - C中使用getchar存储用户输入的多个字符

c++ - 初学者 C "=="总是评估为 false

c++ - C++中的链表相乘

c - Fortran 派生类型包含可从 C 访问的派生类型

c++ - 查看对象是什么类

python - 在 C 中嵌入 Python : unable to read python return integer value