Pointer in C++ language is a variable, it is also known as locator or indicator that points to an address of a value.
Advantages of Pointer:-
- Pointer improves the performance.
- We can return multiple values from function using pointer.
- It makes you able to access any memory location in the computer’s memory.
& (ampersand sign) - The address operator is used to determine the address of a variable.
∗ (asterisk sign) - Indirection operator is used to access the value of an address.
int main()
{
int number=30;
int ∗p;
p=&number;
cout<<"Address of number variable is:"<<&number<<endl;
cout<<"Address of p variable is:"<<p<<endl;
cout<<"Value of p variable is:"<<*p<<endl;
return 0;
}
Sizeof() is an operator that evaluates the size of data type, constants, variable
std::cout << "Size of integer data type : " <<sizeof(int)<< std::endl;
std::cout << "Size of float data type : " <<sizeof(float)<< std::endl;
A void pointer is a general-purpose pointer that can hold the address of any data type, but it is not associated with any data type, even the address of a variable to the variable of a different data type.
void *ptr; // void pointer declaration
int *ptr1; // integer pointer declaration
int data=10; // integer variable initialization
ptr=&data; // storing the address of data variable in void pointer variable
ptr1=(int *)ptr; // assigning void pointer to integer pointer
std::cout << "The value of *ptr1 is : " <<*ptr1<< std::endl;
return 0;