...

Programming languages - C++

Back to Course

Lesson Description


Lession - #48 C++ Pointers


What are Pointers?

A pointer is a variable whose value is the address of another variable. Like any variable or constant, you should announce a pointer before you can work with it. The general type of a pointer variable announcement is −
 

type *var-name;

 


Here, type is the pointer's base type; it should be a substantial C++ type and var-name is the name of the pointer variable. The asterisk you used to proclaim a pointer is the very asterisk that you use for duplication. Notwithstanding, in this explanation the reference mark is being utilized to assign a variable as a pointer. Following are the valid pointer declaration −

 

 

 

int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

 

 

 

 

Using Pointers in C++

There are not many significant operations, which we will do with the pointers as often as possible. (a>
We characterize a pointer variable. (b>
Assign the location of a variable to a pointer. (c>
Finally access the value at the location accessible in the pointer variable. This is finished by utilizing unary operator * that profits the worth of the variable situated at the location indicated by its operand. Following model utilizes these tasks −

 

 

 

 

#include 

using namespace std;

int main (>
{ int var = 20; // actual variable declaration. int *ip; // pointer variable ip = &var; // store address of var in pointer variable cout << "Value of var variable: "; cout << var << endl; // print the address stored in ip pointer variable cout << "Address stored in ip variable: "; cout << ip << endl; // access the value at the address available in pointer cout << "Value of *ip variable: "; cout << *ip << endl; return 0; }

 

 

 

 

Pointers in C++

 

 

Sr.No Concept & Description
1 Null Pointers
C++ supports null pointer, which is a constant with a value of zero defined in several standard libraries.
2 Pointer Arithmetic
There are four arithmetic operators that can be used on pointers: ++, --, +, -
3 Pointers vs Arrays
There is a close relationship between pointers and arrays.
4 Array of Pointers
You can define arrays to hold a number of pointers.
5 Pointer to Pointer
C++ allows you to have pointer on a pointer and so on.
6 Passing Pointers to Functions
Passing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called function.
7 Return Pointer from Functions
C++ allows a function to return a pointer to local variable, static variable and dynamically allocated memory as well.

 

 


C++ Primer is a comprehensive book for students who are studying the C++ language.

 

 

 


To Solve error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools” You just need to Install Microsoft Visual C++ 14.0.
C language programming example with Output · 1. Hello World Program in C · 2. C program to check whether the given number is positive or negative ·
object oriented java :OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.