指针值差异与预期不符。

huangapple go评论49阅读模式
英文:

Pointer value difference not as expected

问题

以下是翻译好的部分:

为什么以下代码显示4-54而不是*-1*?

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int fun(int *a, int *b){
  *a = *a+*b;
  *b = *a-*b;
}

void main(){
  int a=4;
  int b=5;
  int *p=&a, *q=&b;
  fun(p,q);
  printf("%d\t%d",a,b);
}

求和结果为9(如预期),但为什么差值为4?

英文:

Why does the following code display 4-5 as 4 and not -1?

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;
#include &lt;stdlib.h&gt;

int fun(int *a, int *b){
  *a = *a+*b;
  *b = *a-*b;
}

void main(){
  int a=4;
  int b=5;
  int *p=&amp;a, *q=&amp;b;
  fun(p,q);
  printf(&quot;%d\t%d&quot;,a,b);
}

The sum comes as 9 (as expected), but why does the difference come as 4?

答案1

得分: 2

这将*a + *b的结果赋值给*a,所以在以下代码之后:

*a = *a + *b; // 4 + 5

然后 *a9,所以

*b = *a - *b;

将它变为 *b = 9 - 5,即 4

这个问题与指针无关。如果不使用指针,你仍然会遇到相同的问题:

int a = 4;
int b = 5;
a = a + b; // 将 4 + 5 的结果 (9) 赋值给 a
b = a - b; // 将 9 - 5 的结果 (4) 赋值给 b

还要注意,由于fun 声明返回一个 int 但没有返回任何东西,因此程序具有未定义的行为。

英文:

This assigns the result of *a + *b to *a, so after

*a = *a + *b; // 4 + 5

then *a is 9, so

*b = *a - *b;

makes it *b = 9 - 5, which is 4.

The problem has nothing to do with pointers. You would have the same problem if you did it without:

int a = 4;
int b = 5;
a = a + b; // assign the result of 4 + 5 (9) to a
b = a - b; // assign the result of 9 - 5 (4) to b

Also note that the program has undefined behavior since fun is declared to return an int but it doesn't return anything.

答案2

得分: 2

以下是翻译好的部分:

首先,所有这些头文件

#include <string.h>
#include <math.h>
#include <stdlib.h>

都是多余的,因为程序中未使用这些头文件中的任何声明。

根据C标准,无参数的main函数应声明如下:

int main(void)

函数fun的返回类型是int,但未返回任何内容。

应该声明如下:

void fun(int *a, int *b)

至于你的问题,在函数的第一条语句之后:

*a = *a + *b;

对象*a已经被更改,在下一条语句中:

*b = *a - *b;

已经使用了对象的新值。

相反,你可以这样写,例如:

void fun(int *a, int *b)
{
    int tmp = *a;

    *a = *a + *b;
    *b = tmp - *b;
}
英文:

For starters all these headers

#include &lt;string.h&gt;
#include &lt;math.h&gt;
#include &lt;stdlib.h&gt;

are redundant because neither declaration from the headers is used in the program.

Further according to the C Standard the function main without parameters shall be declared like

int main( void )

The function fun has return type int but returns nothing.

int fun(int *a, int *b){

It should be declared like

void fun(int *a, int *b){

As for your problem then after the first statement of the function

*a = *a+*b;

the object *a was changed and in the next statement

*b = *a-*b;

there is used already a new value of the object.

Instead you could write for example

void fun( int *a, int *b )
{
    int tmp = *a;

    *a = *a + *b;
    *b = tmp - *b;
}

答案3

得分: 1

If a=4 and b=5:

int fun(int *a, int *b){
   *a = *a+*b;
   *b = *a-*b;
}

This function would:

  1. [a=4 and b=5]: 第一行将 a 设置为 9 = (4+5)
  2. [a=9 and b=5]: 第二行将 b 设置为 4 = (9-5)

Try this instead:

void fun(int *a, int *b){
   int temp = *a+*b;
   *b = *a-*b;
   *a = temp;
}
英文:

If a=4 and b=5:

int fun(int *a, int *b){
   *a = *a+*b;
   *b = *a-*b;
}

This function would:

  1. [a=4 and b=5]: the first line sets a to 9 = (4+5)
  2. [a=9 and b=5]: the second line sets b to 4 = (9-5)

Try this instead:

void fun(int *a, int *b){
   int temp = *a+*b;
   *b = *a-*b;
   *a = temp;
}

huangapple
  • 本文由 发表于 2023年6月12日 01:31:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定