bat 查找结果输出给变量_C ++参考变量| 查找输出程序| 套装1

本文介绍了C++中指针和引用的概念,通过三个程序展示了如何使用指针和引用进行操作。在程序中,指针用于存储变量的地址,而引用作为变量的别名。通过示例解释了运算符优先级,以及在32位编译器中整型变量的字节数,帮助理解不同操作的影响。
摘要由CSDN通过智能技术生成

bat 查找结果输出给变量

Program 1:

程序1:

#include <iostream>
using namespace std;

int main()
{
    int A = 10;
    int& A1 = A;

    int B = 20, &B1 = B;
    int* ptr;

    ptr = &B1;
    ptr++;

    ptr = &A1;
    ptr++;

    cout << A << " " << B << " ";

    return 0;
}

Output:

输出:

10 20

Explanation:

说明:

Here, we took two variables A and B, created the reference variable for both A1 and B1, and declared an integer pointer, initially, it points to B1, it means it contains the address of B. then we increased the pointer (It will not increase the value).

在这里,我们采用了两个变量AB ,分别为A1B1创建了参考变量,并声明了一个整数指针,该指针最初指向B1 ,这意味着它包含B的地址。 然后我们增加了指针(它不会增加值)。

After that, pointer ptr is pointing to A1, it will contain the address of A1 and A, and will increase the pointer in the same way, values of B or B1 will not increase.

此后,指针ptr指向A1 ,它将包含A1A的地址,并且将以相同的方式增加指针,而BB1的值不会增加。

Then the final values of A and B remain the same. then "10 20" will be printed on the console screen.

然后,A和B的最终值保持不变。 然后“ 10 20”将被打印在控制台屏幕上。

Program 2:

程式2:

#include <iostream>
using namespace std;

int main()
{
    int A = 10;
    int& A1 = A;

    int B = 20, &B1 = B;
    int C = 0;

    C = B1 & A + A1 & B;

    cout << C;

    return 0;
}

Output:

输出:

20

Explanation:

说明:

Here, we took two variables A and B, and created the reference variable for both A1 and B1. The initial value of A and B are 10 and 20 respectively.

在这里,我们采用了两个变量AB ,并为A1B1创建了参考变量。 AB的初始值分别是10和20。

We declared one more variable called C.

我们声明了另一个变量C。

Now evaluate the below expression,

现在评估以下表达式,

C = B1 & A + A1 & B;
C = 20 & 10 + 10 & B;

Here we used two operators + and &, according to operator priority table, + plus will execute before bitwise AND (&). Thus,

这里我们使用了两个运算符+ ,根据运算符优先级表, +加号将在按位AND(&)之前执行。 从而,

C = 20 & 20 & 20;

Now, perform bitwise AND operation, the binary value of 20 is "0001 0100".

现在,执行按位与运算,二进制值20为“ 0001 0100”。

0001 0100
0001 0100
=========
0001 0100

The result of the Bitwise AND operation will be 20. Then,

按位与运算的结果将为20。然后,

C = 20 & 20;
C = 20;

Then the final output will be 20.

最终输出将为20

Program 3:

程式3:

#include <iostream>
using namespace std;

int main()
{
    int A = 10;

    int& A1 = A;
    int& B1 = A1;

    int C = 0;

    C = sizeof((A, A1, B1)) + 5;

    cout << A << " " << A1 << " " << B1 << " " << C;

    return 0;
}

Output:

输出:

10 10 10 9

Explanation:

说明:

Here, we declared a variable with initial value 10, and then we created a reference variable of A that is A1 and we created one more reference variable B1 of A1, so we can say that A1 and B1 are alias name of variable A.

在这里,我们声明的初始值10的变量,然后我们创建了一个的参考变量,A1和我们创建A1的多个参考变量B1,所以我们可以说,A1B1是变量A的别名。

We created one more variable C with initial value 0.

我们还创建了一个初始值为0的变量C。

Now, evaluate the expression,

现在,计算表达式

C = sizeof((A,A1,B1)) +5;

Now, first we evaluate (A, A1, B1), here all three have the same value because A1 and B1 are alias of A.

现在,我们首先求值( AA1B1 ),这三个值都相同,因为A1B1A的别名。

(10, 10 , 10)

If we evaluate the above expression it will return last-most 10.

如果我们评估以上表达式,它将返回最近的10。

Now,

现在,

C= sizeof(10) + 5;

As we already mentioned we are using a 32-bit compiler then the size of any integer will be 4 bytes.

如前所述,我们使用的是32位编译器,则任何整数的大小均为4个字节。

C = 4+5;
C = 9;

Then the final result will be: 10 10 10 9

那么最终结果将是: 10 10 10 9

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/cpp-tutorial/reference-variable-find-output-programs-set-1.aspx

bat 查找结果输出给变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值