小编Sil*_*con的帖子

为什么void返回一个值?

我无法理解一件奇怪的事情.这是我的计划:

#include <iostream>
using namespace std;

void Swap(int *a , int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

int main()
{
    int a=0;
    int b=0;

    cout<<"Please enter integer A: ";
    cin>>a;

    cout<<"Please enter integer B: ";
    cin>>b;

    cout<<endl;

    cout<<"************Before Swap************\n";
    cout<<"Value of A ="<<a<<endl;
    cout<<"Value of B ="<<b<<endl;

    Swap (&a , &b);

    cout<<endl;

    cout<<"************After Swap*************\n";
    cout<<"Value of A ="<<a<<endl;
    cout<<"Value of B ="<<b<<endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,如果你看一下"Swap"这个函数,我就用了"void Swap".因此它不能向main函数返回任何值(只有"int"返回一个值(至少这是我老师教给我的)).但是如果你执行它,值将在主函数中交换!怎么会 ?有谁能告诉我它是如何可能的?

c++ void

5
推荐指数
2
解决办法
194
查看次数

字符类型变量的地址

这只是一个测试原型:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   int a=10;
   char b='H';
   string c="Hamza";

   cout<<"The value of a is : "<<a<<endl;
   cout<<"The value of b is : "<<b<<endl;
   cout<<"The value of c is : "<<c<<endl<<endl;

   cout<<"address of a : "<<&a<<endl;
   cout<<"address of b : "<<&b<<endl;
   cout<<"address of c : "<<&c<<endl;

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么变量'b'的地址是字符类型,而不是打印?

c++ character memory-address

3
推荐指数
1
解决办法
84
查看次数

标签 统计

c++ ×2

character ×1

memory-address ×1

void ×1