Because pointers can be ugh
To understand a pointer, let's review "regular" variables first. If you're familiar with a programming language without pointers like JavaScript, this is what you think when you hear "variable".
When declaring a variable by identifier (or name), the variable is synonymous with its value.
int number = 3;
std::cout << "number is initialized with a value of " << number << "\n";
// Outputs: number is initialized with a value of 3
The value of the variable can be modified directly.
number++;
number = number * 2;
std::cout << "After modifying number, its value is now " << number << "\n";
// Outputs: After modifying number, its value is now 8
Every value lives at memory addresses. The memory address of a variable can be accessed using the &
operator.
std::cout << "The number variable's value lives at memory address " << &number << "\n";
A pointer is a variable that holds a memory address where a value lives. [ need a super brief description of why pointers are useful here ]
A pointer is a variable that holds a memory address where a value lives.
A pointer is declared using the *
operator before an identifier.
As C++ is a statically typed language, the type is required to declare a pointer. This is the type of data that will live at the memory address it points to.
int *numberPointer;
std::cout << "The numberPointer points to the memory address " << numberPointer << ".\n";
// Outputs: The numberPointer points to the memory address 0x0
We've initialized a pointer, but it points nowhere, it has no memory address. Note that 0x0
is equivalent to 0
. It's just the hexadecimal representation of 0
, which memory addresses are expressed in.
We can set the memory address the pointer points to using the memory address of the number
variable.
The memory address of any variable can be accessed using the &
operator.
numberPointer = &number;
std::cout << "The numberPointer points to the memory address `" << numberPointer << "`, which is the memory address of number\n";
// Outputs: The numberPointer points to the memory address 0x1001054a0
// NOTE: if you run this code, you will get a different memory address.
When declaring a pointer, we can alternatively instantiate a variable at its memory address.
int *numberPointer = new int(3);
Instantiating a variable with the new operator always returns a pointer.
The value at the memory address pointed to can be accessed with the dereference operator, *
.
*numberPointer = 4;
std::cout << "The variable's value has been modified using the pointer. The variable's value is now " << number << "\n";
// Outputs: The variable's value has been modified using the pointer. The variable's value is now 4
We've shown that the asterisk operator *
has two semantic meanings: to declare a pointer variable (i.e. int *numberPointer
) and also to access the value a pointer points to (*numberPointer = 4
). This is confusing but just part of the syntax we need to internalize.
In C++, variables are passed to a function by value. When calling a function with an argument, a new variable is instantiated internally and assigned the value passed in the function call.
Any modifications to the value inside the function are performed to this new variable; the variable that was invoked with the function call is unchanged.
void setValueToFive(int x)
{
std::cout << &x << "\n";
x = 5;
}
int x = 3;
setValueToFive( x );
std::cout << "The value of x is " << x << ", setValueToFive() did not modify x.\n";
// Outputs: The value of x is 3, setValueToFive() did not modify x
A function can be written to perform the same task but instead accept a pointer as the argument. This lowers the memory footprint of the program. Unnecessary duplicate variables aren't created. The function can modify the variable's value directly. Any modifications to the variable in the function affect the variable here in this scope too.
void setPointerValueToFive(int *x)
{
*x = 5;
}
int x = 3;
setPointerValueToFive( &x );
std::cout << "The value of x is " << x << ", setPointerValueToFive() modified x.\n";
// Outputs: The value of x is 5, setPointerValueToFive() modified x
In C++ (not in C), you can pass a variable by reference by passing in a variable,
and defining the function's parameter like int &x
. Any modifications to the variable inside the function will also affect the variable here in this scope too.
void setValueToFiveWithReference(int& x)
{
x = 5;
}
int x = 3;
setValueToFiveWithReference( x );
std::cout << "The value of x is " << x << ", setPointerValueToFive() modified x.\n";
// Outputs: The value of x is 5, setPointerValueToFive() modified x
Which to use is a matter of style, and whether you want to perform pointer arithmetic in the function. Google has specific guidelines for which to use where.
The pointer is a variable in itself, and all variables are stored somewhere in memory.
We can see the memory address the pointer's memory address is stored.
Pointers in c++-: Pointer is one of the key aspects of c++ language similar to that of c programming. Pointer offers a unique approach to handle data in c and c++ language.
We know that a pointer is a derived data type that refers to another data variable by storing the variable memory address rather than data. a pointer variable define where to get the value of a specific data variable instead of defining actual data .
Like the c programming language variable can also refer another pointer in c++. However it often point to a data variable . Pointer also provide an alternative approach to access other data object.
Defining a pointer in c++ -: Like any other programming language before we use the pointer in c++ program it is necessary to defining. We can declare a pointer variable similar to the variable in c++. Like c language variable . The declaration is based on the data type of the variable it point to the declaration of a pointer variable takes like this
data-type * pointer-variable;
In this example the pointer variable is the name of the pointer and data type refer to the one of the valid c++ data type such as int,char,float and so many more. The data type is followed by an asterisk (*) which distinguishes a pointer variable from other variable to the computer.
As we know a pointer variable can point to any type of data available in c++. It is necessary to understand that a pointer is able to point to only one data type at the specific time. Let us declare a pointer variable which points to an integer variable as like
int *ptr ;
here ptr is a pointer variable and point to an integer data type. the pointer variable ptr should contain memory location of any integer variable. in the same manner we can declare pointer variable for other data type also.
more about pointer