Pointer Declaration in C++
The most potent C/C++ language characteristics enable developers to interact with storage to handle storage efficiently—the most essential and restricted asset in a computer—for optimum efficiency. These characteristics include pointers, references, and dynamically allocated memory. However, the "pointer" component of C/C++ programming is also particularly intricate and challenging.
Pointers are strong since they provide access to addresses and the ability to change what they contain. But handling them is incredibly difficult as well. They could significantly increase effectiveness and efficiency if used properly. However, utilizing them wrong could result in various issues, including illegible and unmaintained code and well-known defects like memory breaches and buffer overruns that could leave your system vulnerable to hacking. By offering automated memory management, several emerging languages (including Java and C#) do away with the pointer syntax to circumvent the drawbacks of pointers.
Pointer Variables
A computer's address and data are both present in a memory area in a computer. The address is an integer that is difficult for developers to utilize straight because it is commonly represented in hexadecimal. Every address generally stores 8 bits, or 1 byte, of information. The meaning of data, which can be anything, including integers, real numbers, characters, or strings, is totally up to the developer.
Older languages for programming (like C) introduced the concept of variable names to lessen the load of programming using numeric addresses and programmer-interpreted data. A variable is a designated area where a value of a specific kind can be kept. Some addresses have names (or identities) linked to them rather than numerical ones. For simplicity of data understanding, types (such as int, double, and char) are additionally connected with the information.
Pointers
A pointer variable, often known as a pointer or just a pointer, is essentially identical to additional variables that can contain data. A pointer keeps an address for memory instead of a conventional variable that keeps something in memory (such as an int, a double, an integer, or a char).
The Declaration of Pointers
Like an ordinary variable, pointers need to be defined before they can be utilized. A * must come at the beginning of the title when defining a pointer. A type (like int and double) and a pointer are related concepts.
Syntax:
t *ptr; // Define ptr as a reference to a value of t in the ptr variable.
// or
type* ptr;
// or
type * ptr;
Example:
int * P; // Create the P pointer variable, which points to an int (an int pointer). // It contains an address. That address holds an int value.
double * P; // Defining a double pointer
Utilizing the Address-Of Operator (&) to initialize pointers
A pointer variable's data is not initialized when it is declared. It has an exact location of "somewhere," which is obviously an invalid address. It's hazardous! A pointer needs to be initialized by having a legitimate address assigned to it. Typically, the address-of-operator (&) is used to accomplish this.
When used on a variable, the address-of operator (&) yields the address of the constant. For instance, if the variable number is an int, the address of the constant number is returned by &number. The address of a variable can be obtained using the address-of operator, and the value of that address can then be assigned to a pointer parameter. For instance,
int n1 = 99; // An int parameter with a value of 99
int * pN; // Define a pointer variable called pN pointing to an int (or int pointer)
pNum= &n; // Provide pointer pNum the location of the variable number.
int * pA = &n; // Create a second int pointer and initialize it to the variable number's address.
Example code:
#include <iostream>
int main() {
int digit = 67;
int *p = &digit;
std::cout << "digit variable holds the value of : " << digit << std::endl;
std::cout << "Reference of digit variable: " << &digit << std::endl;
std::cout << "Pointer p holds the value as: " << p << std::endl;
std::cout << "Exact value pointed to the digit value by pointer p is: " << *p << std::endl;
*p = 160; // modidfing the value using the pointer
std::cout << "Value of digit after using pointer: " << digit << std::endl;
return 0;
}
Output:
![Pointer Declaration in C++](https://static.tutorialandexample.com/cpp/pointer-declaration-in-cpp1.png)