passing reference in c++
C++
// C++ program to demonstrate differences between pointer
// and reference.
#include <iostream>
using namespace std;
struct demo
{
int a;
};
int main()
{
int x = 5;
int y = 6;
demo d;
int *p;
p = &x;
p = &y; // 1. Pointer reintialization allowed
int &r = x;
// &r = y; // 1. Compile Error
r = y; // 1. x value becomes 6
p = NULL;
// &r = NULL; // 2. Compile Error
p++; // 3. Points to next memory location
r++; // 3. x values becomes 7
cout << &p << " " << &x << endl; // 4. Different address
cout << &r << " " << &x << endl; // 4. Same address
demo *q = &d;
demo &qq = d;
q->a = 8;
// q.a = 8; // 5. Compile Error
qq.a = 8;
// qq->a = 8; // 5. Compile Error
cout << p << endl; // 6. Prints the address
cout << r << endl; // 6. Print the value of x
return 0;
}
Also in C++:
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- string to number in c++
- Category
- C++
- Title
- tuple c++
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- function for searching in map in c++
- Category
- C++
- Title
- how to make a n*n 2d dynamic array in c++
- Category
- C++
- Title
- lower bound c++ for array in decreasing order
- Category
- C++
- Title
- set in c++
- Category
- C++
- Title
- c++ give options string
- Category
- C++
- Title
- ue4 c++ array
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- roscpp publish int32
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- loop through words in string c++
- Category
- C++
- Title
- std::substring
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- check an stack is empty c++
- Category
- C++
- Title
- c++ loop through int array
- Category
- C++
- Title
- kruskal c++
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- C++ w3schools
- Category
- C++
- Title
- GetCurrentThreadId c
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- for loop
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- c++ print one line to console instead of multiple
- Category
- C++
- Title
- visual studio 2019 c++ tutorial project
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- double to string c++
- Category
- C++
- Title
- lambda c++
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- cheap hosting
- Category
- C++
- Title
- using namespace std in c++
- Category
- C++
- Title
- exponenciacion binaria
- Category
- C++
- Title
- inconsequential meaning
- Category
- C++
- Title
- c++ class constructor
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- Merge k sorted linked lists and return it as one sorted list.
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- binary search in set c++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- sfml basic program
- Category
- C++
- Title
- if not defined c++
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- clear qlayout
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- what is atoi in strinf
- Category
- C++
- Title
- check if intent has extras
- Category
- C++
- Title
- how to make a switch case statement in c++
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- console colors in C++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- Shortest Distance in a Maze
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- array sort c++
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- c++ class method example
- Category
- C++
- Title
- dijkstra c++ geeksforgeeks using set
- Category
- C++
- Title
- expected unqualified-id before 'if'
- Category
- C++
- Title
- heredar constructor c++
- Category
- C++
- Title
- c++ remove space from string
- Category
- C++
- Title
- appending a double to a string c++
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- function declerations in C++
- Category
- C++
- Title
- c++ smart pointer 2d array
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- how to compare two strings lexicographically in c++
- Category
- C++
- Title
- gfg bottom view of tree
- Category
- C++
- Title
- c++ delet from memory
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- declaring vector c++
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- find_if c++
- Category
- C++