C中的指针,不在下面的代码中

时间:2020-12-05 20:34:42

What should be the output of the code below? I cant understand what *Y-- do. If someone can explain what *Y do this strange thin in F1. Thanks in advance.

以下代码的输出应该是什么?我不明白* Y--做什么。如果有人能解释一下* Y在F1中做到这么奇怪。提前致谢。

int F1(int , int *);
int A = 3;
int B = 7;
int C = 4; 
int D = 2;

void main(void)
{

    A = F1 (C, &D);
    printf("\n%d %d %d %d", A, B, C, D);
    C = 3;
    C = F1 (A, &C);
    printf("\n%d %d %d %d", A, B, C, D);
}

int F1(int X, int *Y)
{
    int A;
    A = X * *Y;
    C++;
    B += *Y;
    printf("\n%d %d %d %d", A, B, C, D);
    *Y--;
    return (C);
}

with CodeBlocks the output is :

使用CodeBlocks,输出是:

8 9 5 2
5 9 5 2
15 13 4 2
5 13 4 2

But I dont understand why 13, if I have B=B+*Y ...B=12 (?)

但我不明白为什么13,如果我有B = B + * Y ... B = 12(?)

1 个解决方案

#1


0  

A = 5, B = 9, C = 3, D = 2 before calling C = F1(A, &C) ;

在调用C = F1(A,&C)之前,A = 5,B = 9,C = 3,D = 2;

when we make call to F1(A, &C) see here we are passing address of C, now Y is actually referencing to C .

当我们调用F1(A,&C)时,看到这里我们传递的是C的地址,现在Y实际上是在引用C。

Instruction Execution in function call

函数调用中的指令执行

local A variable = 5 * 3 (value at which Y pointing)

local A变量= 5 * 3(Y指向的值)

C++, Global C now becomes 4 ;

C ++,Global C现在变为4;

B += *Y (value at which Y pointing. this will be 4 because of previous C ++)

B + = * Y(Y指向的值。由于之前的C ++,这将是4)

so B = 9 + 4. It is the reason for 13 in third line .

所以B = 9 + 4.这是第三行13的原因。

#1


0  

A = 5, B = 9, C = 3, D = 2 before calling C = F1(A, &C) ;

在调用C = F1(A,&C)之前,A = 5,B = 9,C = 3,D = 2;

when we make call to F1(A, &C) see here we are passing address of C, now Y is actually referencing to C .

当我们调用F1(A,&C)时,看到这里我们传递的是C的地址,现在Y实际上是在引用C。

Instruction Execution in function call

函数调用中的指令执行

local A variable = 5 * 3 (value at which Y pointing)

local A变量= 5 * 3(Y指向的值)

C++, Global C now becomes 4 ;

C ++,Global C现在变为4;

B += *Y (value at which Y pointing. this will be 4 because of previous C ++)

B + = * Y(Y指向的值。由于之前的C ++,这将是4)

so B = 9 + 4. It is the reason for 13 in third line .

所以B = 9 + 4.这是第三行13的原因。