codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ references

C++ references

Another name for a variable.

A reference (int &r = x;) must be set when created and always points to the same variable. Changing it changes the original.

Example code

#include <iostream>
using namespace std;
int main() {
  int x = 5;
  int &r = x;
  r = 9;
  cout << x << " " << r << endl;
  return 0;
}

Run this code in the online editor

← C++ structures and enumsC++ pointers →