C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ 引用与指针

C++参考与指针似乎很相似,但是它们之间存在一些差异。引用是一个变量,它是现有变量的另一个名称,而指针是存储另一个变量地址的变量。

什么是引用?

引用是变量,它是已经存在的变量的另一个名称。变量的引用是通过存储另一个变量的地址来创建的。
可以将引用变量视为具有自动间接指向性的常量指针。在这里,自动间接意味着编译器会自动应用间接运算符(*)。
参考示例:
int &a = i;
在上面的声明中," a"是" i"变量的别名。我们也可以通过'a'变量来引用'i'变量。
让我们通过示例来理解。
#include <iostream>
using namespace std;
int main()
{  
   int i=8;    // variable initialization
   int &a=i; // creating a reference variable
   cout<<"The value of 'i' variable is :"<<a;
   return 0;
}
在上面的代码中,我们创建了一个参考变量,即'a'表示'i'变量。创建参考变量后,我们可以在'a'变量的帮助下访问'i'的值。

什么是指针?

指针是包含另一个变量地址的变量。可以在(*) operator 的帮助下取消引用,以访问指针指向的内存位置。

引用和指针之间的区别

以下是引用和指针之间的区别:
定义
参考变量是已经存在的变量的另一个名称。它主要用在" 按引用传递"中,其中,将引用变量作为参数传递给函数,并且将该变量传递给的函数在该变量的原始副本上起作用。
让我们通过一个简单的例子来理解。
#include <iostream>
using namespace std;
void func(int &);
int main()
{
   int a=10;
   std::cout <<"Value of 'a' is :" <<a<< std::endl;
  func(a); 
  std::cout << "Now value of 'a' is :" <<a<< std::endl;
  return 0;
}
void func(int &m)
{
   m=8;
}
输出:
Value of 'a' is :10                                                                                                             
Now value of 'a' is :8  
而指针是一个变量,用于存储另一个变量的地址。因为它保留了一些变量的内存地址,所以使编程更加容易。
声明
我们可以通过在变量之前添加"&"符号来声明参考变量。如果在表达式中使用此符号,则将其视为地址运算符。
在使用指针变量之前,我们应声明一个指针变量,并通过添加一个'*'来创建此变量。变量前的运算符。
重新分配
我们无法重新分配参考变量。现在,我们举一个简单的例子,如下所示:
#include <iostream>
using namespace std;
void func(int &);
int main()
{
  int i;    // variable declaration
  int k;    // variable declaration
  int &a=i;
  int &a=k; // error
  return 0;
}
上面的代码显示错误,不允许多个 int&a 声明。因此,以上程序得出结论,对于参考变量,重新分配操作无效。
其中,可以重新分配指针。当我们使用链表,树等数据结构时,这种重新分配非常有用。
内存地址
在引用的情况下,引用和实际变量都引用相同的地址。在实际变量被删​​除或超出范围之前,不会将新变量分配给参考变量。
让我们通过示例了解这种情况。
#include <iostream>
using namespace std;
void func(int &);
int main()
{
  int i;
  int &a=i;
  std::cout << "The address of 'a' variable is : " <<&a<< std::endl;
  std::cout << "The address of 'i' variable is : " <<&i<< std::endl;
  return 0;
}
输出:
The address of 'a' variable is : 0x7fff078e7e44                                                                        The address of 'i' variable is : 0x7fff078e7e4
上面的输出显示参考变量和实际变量都具有相同的地址。
对于指针,指针变量和实际变量都将具有不同的内存地址。让我们通过一个例子来理解这一点。
#include <iostream>
using namespace std;
int main()
{
    int k;
    int *p;
    p=&k;
    cout<<"The memory address of p variable is :"<<&p;
    cout<<"\nThe memory address of k variable is :"<<&k;
    return 0;
}
输出:
The memory address of p variable is :0x7ffcc5c164b8                                                           The memory address of k variable is :0x7ffcc5c164b4 
空值
我们不能将NULL值分配给引用变量,但是可以为指针变量分配NULL值。
间接
指针可以具有指向提供不止一级间接性的指针。
#include <iostream>
using namespace std;
int main()
{
 int *p;
 int a=8;
 int **q;
 p=&a;
 q=&p;
std::cout << "The value of q is : " <<*q<< std::endl;
return 0;
}
在上面的代码中,指针" p"指向变量" a",而" q"是指向" p"的双指针。因此,我们可以说'p'的值将是'a'变量的地址,而'q'的值将是'p'变量的地址。
输出:
The value of q is : 0x7ffd104891dc  
在使用引用的情况下,无法引用引用。如果我们尝试执行 C++程序,则会抛出编译时错误
让我们理解通过示例了解这种情况。
#include <iostream>
using namespace std;
int main()
{
  int a=8; // variable initialization
 int &p=a; // creating a reference variable for ?a? variable.
 int &&q=p;  // reference to reference is not valid, it throws an error.
 return 0;
}
输出:
main.cpp: In function 'int main()':
main.cpp:18:10: error: cannot bind 'int' lvalue to 'int&&'
int &&q=p;
算术运算
我们知道算术运算可以应用于名为" Pointer Arithmetic "的指针,但是算术运算不能应用于引用。没有字,即 C++ 中存在引用算法。
让我们看看指针的简单示例。
#include <iostream>
using namespace std;
int main()
{
 int a[]={1,2,3,4,5}; // array initialization
  int *ptr;  // pointer declaration
  ptr=a; assigning base address to pointer ptr.
  cout<<"The value of *ptr is :"<<*ptr;
  ptr=ptr+1;  // incrementing the value of ptr by 1.
  std::cout << "\nThe value of *ptr is: " <<*ptr<< std::endl;
  return 0;
}
输出:
The value of *ptr is :1                                                                                                       
The value of *ptr is: 2
让我们通过示例了解参考。
#include <iostream>
using namespace std;
int main()
{
 
 int value=90;  // variable declaration
 int &a=value;   // assigning value to the reference
 &a=&a+5 // arithmetic operation is not possible with reference variable, it throws an error.
 return 0;
}
由于引用不允许进行算术运算,因此上面的代码将引发编译时错误。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4