Pointers and References in C++ - GeeksforGeeks

01 Jun, 2021
Pointers in C++: Pointers are a symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Its general declaration in C/C++ has the format: Syntax:  
Pointers in C++: Pointers are a symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Its general declaration in C/C++ has the format: Syntax:  
datatype *var_name; 
Example:  
Example:  
Example:  
// ptr can point to an address
// which holds int data
int *ptr;   
 More on Pointers in C++: This article would provide a further insight into Pointers, on how it works and explains the mathematical background of it. Pointers store address of variables or a memory location.  
 
 
More on Pointers in C++: This article would provide a further insight into Pointers, on how it works and explains the mathematical background of it. Pointers store address of variables or a memory location.  
 
// General syntax
datatype *var_name; 

// An example pointer "ptr" that holds
// address of an integer variable or holds
// address of a memory whose value(s) can
// be accessed as integer values through "ptr"
int *ptr;
 Applications of Pointers in C/C++: A pointer has various applications like: To pass arguments by reference: Passing by reference serves two purposesFor accessing array elements: Compiler internally uses pointers to access array elements.To return multiple values: For example in returning square and the square root of numbers.Dynamic memory allocation: We can use pointers to dynamically allocate memory. The advantage of dynamically allocated memory is, it is not deleted until we explicitly delete it.To implement data structures.To do system-level programming where memory addresses are useful.Features and Use of Pointers in C/C++: The Pointers share few important features like it saves memory space, they are used to allocate memory dynamically, it is used for file handling etc. Pointers store address of variables or a memory location. Syntax:  
 
Applications of Pointers in C/C++: A pointer has various applications like: To pass arguments by reference: Passing by reference serves two purposesFor accessing array elements: Compiler internally uses pointers to access array elements.To return multiple values: For example in returning square and the square root of numbers.Dynamic memory allocation: We can use pointers to dynamically allocate memory. The advantage of dynamically allocated memory is, it is not deleted until we explicitly delete it.To implement data structures.To do system-level programming where memory addresses are useful.
To pass arguments by reference: Passing by reference serves two purposes
For accessing array elements: Compiler internally uses pointers to access array elements.
To return multiple values: For example in returning square and the square root of numbers.
Dynamic memory allocation: We can use pointers to dynamically allocate memory. The advantage of dynamically allocated memory is, it is not deleted until we explicitly delete it.
To implement data structures.
To do system-level programming where memory addresses are useful.
Features and Use of Pointers in C/C++: The Pointers share few important features like it saves memory space, they are used to allocate memory dynamically, it is used for file handling etc. Pointers store address of variables or a memory location. Syntax:  
datatype *var_name; 
Example: pointer “ptr” holds the address of an integer variable or holds the address of memory whose value(s) can be accessed as integer values through “ptr”  
Example: pointer “ptr” holds the address of an integer variable or holds the address of memory whose value(s) can be accessed as integer values through “ptr”  
 
int *ptr;
 ‘this’ Pointer in C++ The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name). Even if only one member of each function exists which is used by multiple objects, the compiler supplies an implicit pointer along with the names of the functions as ‘this’. Declaration:  
 
‘this’ Pointer in C++ The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name). Even if only one member of each function exists which is used by multiple objects, the compiler supplies an implicit pointer along with the names of the functions as ‘this’. Declaration:  
this->x = x; 
 References in C++: When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration. There are 3 ways to pass C++ arguments to a function: call-by-valuecall-by-reference with pointer argumentcall-by-reference with reference argumentPointers vs References in C++: This article lays proper ground of differences between Pointer and Reference. Both references and pointers can be used to change local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain. Despite the above similarities, there are the following differences between references and pointers. A pointer can be declared as void but a reference can never be void Example:  
 
References in C++: When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration. There are 3 ways to pass C++ arguments to a function: call-by-valuecall-by-reference with pointer argumentcall-by-reference with reference argument
call-by-value
call-by-reference with pointer argument
call-by-reference with reference argument
Pointers vs References in C++: This article lays proper ground of differences between Pointer and Reference. Both references and pointers can be used to change local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain. Despite the above similarities, there are the following differences between references and pointers. A pointer can be declared as void but a reference can never be void Example:  
A pointer can be declared as void but a reference can never be void Example:  
 
int a = 10;
void* aa = &a;. //it is valid
void &ar = a; // it is not valid
References are less powerful than pointers
Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing.
A reference must be initialized when declared. There is no such restriction with pointers
Passing by pointer Vs Passing by Reference in C++: In C++, we can pass parameters to a function either by pointers or by reference. In both the cases, we get the same result. So what should be preferred and why?Passing Reference to a Pointer in C++: In this article let’s compare the usage of a “pointer to pointer” VS “Reference to pointer” in some cases.
Passing by pointer Vs Passing by Reference in C++: In C++, we can pass parameters to a function either by pointers or by reference. In both the cases, we get the same result. So what should be preferred and why?
Passing Reference to a Pointer in C++: In this article let’s compare the usage of a “pointer to pointer” VS “Reference to pointer” in some cases.
simmytarika5
cpp-pointer
cpp-references
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Operator Overloading in C++
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
List in C++ Standard Template Library (STL)
Convert string to char array in C++
Pair in C++ Standard Template Library (STL)
Destructors in C++
new and delete operators in C++ for dynamic memory
Queue in C++ Standard Template Library (STL)